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();
7 Answers
+ 5
Kei Frap
Don't return just print.
this.print = function () {
console.log(this.name + ": " + this.number);
}
+ 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.
+ 2
Kei Frap , you are missing the brackets after calling the print method. Change it to a.print() and b.print().
+ 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!
+ 1
It was already in lowercase xhen i opened that but Ty for the advice
Why contact.prototype.print and not contact.print?
+ 1
Martin Taylor
Ty very much for ur time and links
👍😆
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;