0

Contact manager

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.my code: function contact(name, number) { this.name = name; this.number = number; this.print=function (){ console.log(name, number); } } var a = new contact("David", 12345); var b = new contact("Amy", 987654321) a.print(); b.print(); Whats my mistake?

27th Dec 2020, 6:15 AM
Lenoname
12 Answers
+ 6
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);
15th Mar 2021, 1:59 PM
SHUBHAM RAMESH NIMJE
SHUBHAM RAMESH NIMJE - avatar
+ 4
You cannot pass `:` just like that as it will be against the syntax rules of the language. However, you can make it a string (":") and then add name, ":" and number. If you have forgotten, please revise concatenation https://www.sololearn.com/learn/JavaScript/1134/ OR If you want you can also use template string literals which is a newer feature of JavaScript. Go to the following link to see how to use it (go to the 3rd page) https://www.sololearn.com/learn/JavaScript/2969/
27th Dec 2020, 12:30 PM
XXX
XXX - avatar
+ 4
function contact(name, number) { this.name = name; this.number = number; this.print = print; } function print() { console.log(this.name + ": " + this.number); } var a = new contact("David", 12345); var b = new contact("Amy", 7004099941) a.print(); b.print();
17th Aug 2021, 12:15 PM
Swadhin Kumar
Swadhin Kumar - avatar
+ 4
You've been tricked to use a.print B.print Javascript does not have any print method
3rd Sep 2021, 6:28 AM
I am DAMI
I am DAMI - avatar
+ 3
Yaser In the question, it is given "output the contact data to the console in the following format name: number" In your code, your are passing the name and number as arguments to console.log, which will by default print them separated by a space, like so name number which does not match the format given in the question. To fix the problem, just output the name and number in the correct format. Remember that strings can be added to each other in JavaScript.
27th Dec 2020, 6:20 AM
XXX
XXX - avatar
0
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();
20th Jan 2021, 5:23 PM
Rajneesh Kumar Singh
0
It's helped me to clear the error I was putting wrong.
20th Jan 2021, 9:37 PM
Himanshu Thakur
Himanshu Thakur - avatar
0
i dont understand
24th Nov 2021, 7:03 PM
jade
0
function contact (name,number) { this.name = name; this.number = number; this.print = printContact; } function printContact() { return console.log(this.name + ": " + this.number) } var a = new contact ("David","12345"); a.print(); var b = new contact ("Amy","987654321"); b.print();
14th Jan 2024, 2:39 AM
Marc Andrew Picardal
Marc Andrew Picardal - avatar
- 1
try this code function contact(name, number) { this.name = name; this.number = number; this.print = () => { console.log(name + ":",number); } } var a = new contact("David", 12345); var b = new contact("Amy", 987654321); a.print(); b.print();
14th Jul 2022, 11:45 AM
Abdu lkerim
Abdu lkerim - avatar
- 2
If I put a colon in console.log() it wont work. Meaning: console.log(name:,number)
27th Dec 2020, 11:16 AM
Lenoname