+ 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());

26th Oct 2021, 3:59 PM
Bishesh Kasera
Bishesh Kasera - avatar
5 Answers
+ 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()
26th Oct 2021, 4:10 PM
Pariket Thakur
Pariket Thakur - avatar
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
26th Oct 2021, 4:05 PM
John Doe
0
Thank you so much
27th Oct 2021, 4:01 PM
Bishesh Kasera
Bishesh Kasera - avatar
0
Thanks for clearing this up for me as well!
28th Oct 2021, 4:48 AM
Stacey Prahl
Stacey Prahl - avatar
0
I redid the code as you indicated, but I am not getting the spaces, so I keep getting an error
28th Oct 2021, 4:59 AM
Stacey Prahl
Stacey Prahl - avatar