+ 1
Where should I add "/n" , for have every output in a line ?
function person(name, age, color) { this.name = name; this.age = age; this.favColor = color; } var p1 = new person("John", 42, "green"); var p2 = new person("Amy", 21, "red"); document.write(p1.age); document.write(p2.name);
2 ответов
+ 3
your browser won't understand "\n".
You need to use break line tag instead, like so
function person(name, age, color) {
this.name = name;
this.age = age;
this.favColor = color;
}
var p1 = new person("John", 42, "green");
var p2 = new person("Amy", 21, "red");
document.write(p1.age+"<br>");
document.write(p2.name+"<br>");
+ 2
Nowhere.
I think you mean \n instead of /n. If you want JavaScript to write HTML, the \n is quite meaningless too. You could write a br tag if you want to break a line.
Consider using console.log instead of document.write if you just want to write out text and see the results while learning to program in JavaScript. console.log output can be viewed in a web browser's JavaScript console.
If you have some useful content in a web page and want to put text in there, consider manipulating the document's elements too. You could do something like:
var paragraph1 = document.querySelector('p'); // assuming you have a p tag in your page
paragraph1.innerText = p1.name;