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?
11 Respostas
+ 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);
+ 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/
+ 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();
+ 4
You've been tricked to use
a.print
B.print
Javascript does not have any print method
+ 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.
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();
0
It's helped me to clear the error I was putting wrong.
0
i dont understand
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();
- 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();
- 2
If I put a colon in console.log() it wont work. Meaning: console.log(name:,number)