创建一个名为”car”的对象,并使其其中一个属性显示一个字符串。

huangapple go评论63阅读模式
英文:

Create an object “car” and have one of its properties display a string

问题

  1. 定义一个名为car的变量,是一个带有以下属性的对象:

    • model'Nissan'

    • color'Blue'

    • numWheels是一个轮子的数量,值为4

  2. 这个对象还应该有以下方法:

    • 方法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!'",你得到了需要的结果。

英文:
  1. Define a variable car, an Object with the following properties:

    • model of ’Nissan’

    • color of ’Blue’

    • numWheels, a number of wheels. Value is 4

  2. The object should also have the following method:

    • Method drive that returns ’Vroom!’

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 returns Vroom!

在你的代码中,你的 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: &#39;Nissan&#39;,
    color: &#39;Blue&#39;,
    numwheels: 4,
    drive: function() {
        return &#39;Vroom!&#39;;
    },
}
console.log (car.drive());

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 05:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定