e4 = prompt("Enter the five favorite sports", "hockey,football,basketball,tennis,golf");
e4 = e4.split(",")
for (var i = 0; i < e4.length; i++) {
if (e4[i] == "football") {
e4[i] = "soccer";
}
}
e5 = prompt("Enter extra sport", "formula 1");
e4.push(e5);
for (var i = 0; i < e4.length; i++) {
e4[i] = e4[i].toUpperCase();
}
e4.sort();
This is my lines of code that would print out like this
["BASKETBALL", "FORMULA 1", "GOLF", "HOCKEY", "SOCCKER", "TENNIS]
however, what I want to print out is in format such as this:
BASKETBALL
FORMULA 1
GOLF
HOCKEY
SOCCER
TENNIS
and new lines of other code would start here.
I'm from C++ so im aware of \n, but how would you format it like that so it is indented like that above and creating new lines everytime it goes through elements in array?
Antti29
3,03912 gold badges37 silver badges36 bronze badges
-
(Welcome to SO! For machine input/output, I prefer block quotes over code blocks.) Have you tried to output characters similar to the example you posted? In which way has the look of that been lacking? Do you print "to HTML"?greybeard– greybeard2017年10月08日 07:01:09 +00:00Commented Oct 8, 2017 at 7:01
2 Answers 2
IMO, this is a perfect use case Array#join
Use <br/> tag as a glue in [].join
var e4 = prompt("Enter the five favorite sports", "hockey,football,basketball,tennis,golf");
e4 = e4.split(",")
for (var i = 0; i < e4.length; i++) {
if (e4[i] == "football") {
e4[i] = "soccer";
}
}
e5 = prompt("Enter extra sport", "formula 1");
e4.push(e5);
for (var i = 0; i < e4.length; i++) {
e4[i] = e4[i].toUpperCase();
}
var sorted = e4.sort().join('<br/>');
document.body.innerHTML = sorted;
answered Oct 8, 2017 at 4:13
Rayon
36.6k5 gold badges54 silver badges78 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can try something like:
e4 = prompt("Enter the five favorite sports", "hockey,football,basketball,tennis,golf");
e4 = e4.split(",");
for (var i = 0; i < e4.length; i++) {
if (e4[i] == "football") {
e4[i] = "soccer";
}
}
e5 = prompt("Enter extra sport", "formula 1");
e4.push(e5);
e4.sort();
for (var i = 0; i < e4.length; i++) {
e4[i] = e4[i].toUpperCase();
if (i == e4.length) {
e4[i] = " " + e4[i];
} else {
e4[i] = " " + e4[i] + "\n";
}
}
console.log(e4.toString().replace(/,/g, ""));
answered Oct 8, 2017 at 4:10
Luka Čelebić
1,09111 silver badges21 bronze badges
Comments
lang-js