JavaScript method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JavaScript method

Hi guys I have a probleme to solve this :) Someone can help me ? Tyy !! 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. What i did below but no way this work ; function contact(name, number) { this.name = name; this.number = number; this.print = function (){ return this.name+": "+this.number} } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(); b.print();

19th Dec 2020, 9:36 AM
Kei Frap
Kei Frap - avatar
7 Answers
+ 5
Kei Frap Don't return just print. this.print = function () { console.log(this.name + ": " + this.number); }
19th Dec 2020, 9:38 AM
A͢J
A͢J - avatar
+ 4
Kei Frap You missed the opening and closing brackets in console.log. It's console.log() not console.log You was supposed to do like this: console.log(this.name + ": " + this.number); I had provided you correct solution.
19th Dec 2020, 11:34 AM
A͢J
A͢J - avatar
+ 2
Kei Frap , you are missing the brackets after calling the print method. Change it to a.print() and b.print().
19th Dec 2020, 10:24 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
My Bad hahahahaha same prob with brackets but i noticed After that i forgot parentheses for console.log Need to be more prudent with these. Ty very much Groot ans whitecat!
19th Dec 2020, 10:50 AM
Kei Frap
Kei Frap - avatar
+ 1
It was already in lowercase xhen i opened that but Ty for the advice Why contact.prototype.print and not contact.print?
19th Dec 2020, 11:21 AM
Kei Frap
Kei Frap - avatar
+ 1
Martin Taylor Ty very much for ur time and links 👍😆
19th Dec 2020, 2:45 PM
Kei Frap
Kei Frap - avatar
0
Hii groot ! Ty for ur reply but it doesnt work too function contact(name, number) { this.name = name; this.number = number; this.print = function (){ console.log this.name+": "+this.number;} } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print; b.print;
19th Dec 2020, 10:05 AM
Kei Frap
Kei Frap - avatar