Ujjwal Batra
Ujjwal Batra

Reputation: 47

Output onto a new line

The following code writes everything onto single line in the browser. I want to show the result of the switch statement on a single line and the rest on a different line.

For example :

var age;
switch(age){
     case age > 13:
        document.write("lela is teenager");
        break;
     case age >=13 && age < 20:
        document.write("lela is young");
        break;
     case age >=20 && age < 30:
        document.write("lela is mature");
        break;
     default:
        document.write("lela is old") ;   
}
     
var scoreJohn = (89 + 120 + 103) / 3;
var scoreMike = (116 + 94 + 123) / 3;
document.write(scoreJohn,scoreMike);

Upvotes: 1

Views: 128

Answers (2)

Nicolas
Nicolas

Reputation: 8650

You could use some <br /> in your string to add cariage returns.

var age;
switch(age){
    case age > 13:
        document.write("lela is teenager <br />");
        break;

    case age >=13 && age < 20:
        document.write("lela is young <br />");

    case age >=20 && age < 30:
        document.write("lela is mature <br />");  

    default:
        document.write("lela is old <br />") ;   
}

var scoreJohn = (89 + 120 + 103) / 3;
var scoreMike = (116 + 94 + 123) / 3;
document.write(scoreJohn, '<br />', scoreMike);

Upvotes: 3

Vishnu
Vishnu

Reputation: 897

you can use a break tag.

var age;
switch(age){
     case age > 13:
        document.write("lela is teenager");
        break;
     case age >=13 && age < 20:
        document.write("lela is young");
        break;
     case age >=20 && age < 30:
        document.write("lela is mature");
        break;
     default:
        document.write("lela is old") ;   
}
     
var scoreJohn = (89 + 120 + 103) / 3;
var scoreMike = (116 + 94 + 123) / 3;
document.write("<br>");

document.write(scoreJohn,scoreMike);

Upvotes: 0

Related Questions