+ 5
[Solved]JS Course
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();
15 odpowiedzi
+ 4
Add to contact function:
this.print = print.
Place a.print() and b.print() into console.log()
+ 4
speedprogame thanks bro🤗
+ 3
Alvaro Bastia No Problem I got the answer already🙃
+ 2
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
+ 2
Lisa thanks ☺️
+ 2
I'm sorry but i still dont understand how to code the print function and to make it print in the final result
+ 2
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()
+ 2
LordOfThunder LordOfThunder Might you help me? I still haven't been able to solve
+ 1
LordOfThunder Might you help me? I still haven't been able to solve
+ 1
Juan Barboza ok
+ 1
Thanks so much Andre Ponce
+ 1
I still cant do it right
0
LordOfThunder I am Juan Barboza, please help me to solve this exercise, I still don't know the answer. I would appreciate
0
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())
0
i got it.
function contact(name, number) {
this.name = name;
this.number = number;
this.print = print
function print (contact)
{
console.log(name + ": " + number)
}
}
var a = new contact("David", 12345);
var b = new contact("Amy", 987654321)
a.print();
b.print();