/    Sign up×
Community /Pin to ProfileBookmark

problem with printing an array

Hi everyone! I am trying to get a line break after every element in my array. In other words, the names I enter I want it to look like:
Name
Name
Name
etc.
Not like: Name, Name, Name, etc.
I have posted my code. Also if anyone see a more efficient way of this script, let me know.
Thanks!

[code]
<?xml version = “1.0” encoding = “utf-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHMTL 1.0 Strict//EN”
“http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd”>

<!– ssort.html
Clint Sharp CS3010-001 XHTML/Javascript Selection Sort
–>
<html xmlns = “http://www.w3.org/1999/xhtml”>

<head>
<title>Clint Sharp’s XHTML/Javascript Selection Sort</title>

<script type = “text/javascript”>
<!–
function displayNames() {
document.write(selectionSort(nameArray));
}

function swap(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}

function selectionSort(items){
var len = items.length,min;

for (i=0; i < len; i++){
min = i;
for (j=i+1; j < len; j++){
if (items[j] < items[min]){
min = j;
}
}

if (i != min){
swap(items, i, min);
}
}
return items;
}

var nameArray = [];
var nameEntry = ”;
do {
nameEntry = prompt(“Enter a name. Press cancel or leave prompt field empty to finish.”);
if (nameEntry === “” || nameEntry === null) {
break;
} else {
nameArray.push(nameEntry);
}
} while (1);

displayNames();
//–>
</script>
</head>
<body>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Feb 25.2012 — function displayNames() {
document.write(selectionSort(nameArray)[B].join("&lt;br&gt;")[/B]);
}

And I strongly suggest that you indent your code correctly, it's much easier to read. while(true) is also probably nicer than while(1).
Copy linkTweet thisAlerts:
@csharp100authorFeb 25.2012 — function displayNames() {
document.write(selectionSort(nameArray)[B].join("&lt;br&gt;")[/B]);
}

And I strongly suggest that you indent your code correctly, it's much easier to read. while(true) is also probably nicer than while(1).[/QUOTE]


Excellent! I was reading that as you were posting. I thought indentation was good but I will do more. Thank for the while(true) as well. I was reading that as well.

Cudos!
×

Success!

Help @csharp100 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...