英文:
Create an object “car” and have one of its properties display a string
问题
-
定义一个名为
car
的变量,是一个带有以下属性的对象:-
model
为'Nissan'
-
color
为'Blue'
-
numWheels
是一个轮子的数量,值为4
-
-
这个对象还应该有以下方法:
- 方法
drive
,返回'Vroom!'
- 方法
你尝试的代码:
const car = {
model: 'Nissan',
color: 'Blue',
numWheels: 4,
drive: function() {
return 'Vroom!';
}
}
console.log(car);
你得到的结果:
{
model: 'Nissan',
color: 'Blue',
numWheels: 4,
drive: [Function: drive]
}
我已将 "console.log ('Vroom!')" 更改为 "return 'Vroom!'",你得到了需要的结果。
英文:
-
Define a variable
car
, an Object with the following properties:-
model
of’Nissan’
-
color
of’Blue’
-
numWheels
, a number of wheels. Value is4
-
-
The object should also have the following method:
- Method
drive
that returns’Vroom!’
- Method
What I tried:
const car = {
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: function() {
return'Vroom!'
},
}
console.log (car)
What I got:
{
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: [Function: drive]
}
I have changed “console.log (‘Vroom!)”
To “return ‘Vroom!’”
And I get the result I need
答案1
得分: 0
- Method
drive
that returnsVroom!
在你的代码中,你的 drive()
方法将 Vroom!
记录到控制台,而不是将其作为函数的返回值。将 console.log('Vroom!')
更改为 return 'Vroom!'
,并使用 console.log(car.drive())
而不是 console.log(car)
。你的完整代码应该类似于以下内容:
const car = {
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: function() {
return 'Vroom!';
},
}
console.log(car.drive());
英文:
> - Method drive
that returns Vroom!
In your code, your drive()
method is logging Vroom!
to the console, not returning it as the value of the function. Change console.log('Vroom!')
to return 'Vroom!'
and use console.log(car.drive())
instead of console.log(car)
. Your complete code should look something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const car = {
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: function() {
return 'Vroom!';
},
}
console.log (car.drive());
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论