0
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?

1
  • (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"? Commented Oct 8, 2017 at 7:01

2 Answers 2

1

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;

Sign up to request clarification or add additional context in comments.

Comments

1

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, ""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.