+ 2

Yo I'm doing the contact manager project and I'm supposed to create a print method and it's just confusing

This is all they gave function contact(name, number) { this.name = name; this.number = number; } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(); b.print(); Then i tried to create a print function to make it output the contacts in the format Name:number But it just keeps saying one thing is not defined or missing parentheses and Ive tried sometimes it gives no output so pls I need help cus I been on this for a few days but I haven't got a solution Also I don't even know if the print is a special word for doing a paticular thing cus when you type it it's colored

3rd Aug 2021, 12:36 PM
David
David - avatar
9 Réponses
+ 3
Okey first of all you can't just add : through return, that's why you had an error but you can print them out directly as follows: function contact(name, number) { this.name = name; this.number = number; } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); console.log(a.name + ": " + a.number); console.log(b.name + ": " + b.number);
3rd Aug 2021, 12:50 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Oh okay thanks a lot
3rd Aug 2021, 12:52 PM
David
David - avatar
0
Could you share your attempt?
3rd Aug 2021, 12:41 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Sure
3rd Aug 2021, 12:42 PM
David
David - avatar
0
i wonder why would you need another function to print them up when you can print them directly. for eg-: console.log(a, b); (well. I'm not a JS expert)
3rd Aug 2021, 12:43 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
function contact(name, number) { this.name = name; this.number = number; } function print(x,y){ return(x: y) } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(a.name a.number); b.print(b.name b.number); console.log(a); console.log(b);
3rd Aug 2021, 12:44 PM
David
David - avatar
0
No I can't print them directly cus ur asked to make a print method to output them like David: 12345 Amy: 987654321
3rd Aug 2021, 12:45 PM
David
David - avatar
0
Besides I tried doing that and it gave contact david 12345 Contact Amy 987654321
3rd Aug 2021, 12:47 PM
David
David - avatar
0
When you make a function one of the biggest objectives is not repeating the same code over and over again. An advatnage of this in the long run is that when you want to change the way something behaves in the future, then you only need to change it in one place. Its also good to make your variables have meaningful names so its more readable. So you can do this this to your print func: function print(contact) { console.log(contact.name + “: “ + contact.number) } var david = new contact(“David”, 12345) print(david)
4th Aug 2021, 5:29 PM
Ryan Mansoor