+ 1
Help me to solve this javascript test in the javascript course. I already wrote the code but what's wrong in the code .
Question You are working on a Contact Manager app. You have created the contact object constructor, which has two arguments, name and number. You need to add a print() method to the object, which will output the contact data to the console in the following format: name: number The given code declares two objects and calls their print() methods. Complete the code by defining the print() method for the objects. My code function contact(name, number) { this.name = name; this.number = number; this.print = print1; } function print () { return console.log(this.name ":" this.number); } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); document.write(a.print()); document.write(b.print());
5 Antworten
+ 2
Your Mistakes
1. this.print = print1 ;
^
print1 is not define
2. Using this.print() = function(){...}
(inside contact function)
3. U should write
console.log(this.name + ":" + this.number )
4. Don't using a.print() inside document.write()
Full code :
function contact(name, number) {
this.name = name;
this.number = number;
//this.print = print1;
this.print = function(){
console.log(this.name+ ":" +this.number);
}
}
/*function print () {
return console.log(this.name ":" this.number);
}*/
var a = new contact("David", 12345);
var b = new contact("Amy", 987654321);
//document.write(a.print());
//document.write(b.print());
a.print()
b.print()
0
this.name ":" this.number
You need to concatenate these three things via an operator, you cant just write them after each other. In javascript string concatenation is done with the + operator.
Also, as Beauty said, remove document write
0
Thank you so much
0
Thanks for clearing this up for me as well!
0
I redid the code as you indicated, but I am not getting the spaces, so I keep getting an error