can you guys tell me what's the bug in this code?(JS Course Contact Manager project) function contact(name, number) { this.name = name; this.number = number; } function print(){ return this.name,this.number; } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print();
12/30/2020 3:16:56 PM
LordOfThunder [Inactive]14 Answers
New AnswerAdd to contact function: this.print = print. Place a.print() and b.print() into console.log()
Your print function has no object with name or number (this) which could refer to. So a) make print function method of the contact object (which is what you seem to have intended), or b) pass the contact object by parameter to the print function
I'm sorry but i still dont understand how to code the print function and to make it print in the final result
Alvaro Bastia The idea is to make print() a method of the contact object. The concept is this: function Obj(x) { this.x = x; print = function(){ console.log(this.x) } } var x = new Obj("Hi"); x.print()
LordOfThunder I am Juan Barboza, please help me to solve this exercise, I still don't know the answer. I would appreciate
Hi guys, I come with the solution function contact(name, number) { this.name = name; this.number = number; this.print = function (name,number) { return this.name +": "+ this.number } } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) console.log(a.print()); console.log(b.print())
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message