Write Concise Declarative Functions. i do not understand what to do

Tell us what’s happening:

Your code so far


// change code below this line
const bicycle = {
  gear: 2,
  setGear: function(newGear) {
    newGear:4;
    "use strict";
    this.gear = newGear;
  }
};
// change code above this line
bicycle.setGear(3);
console.log(bicycle.gear);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6

For quick reference here’s what the challenge is aiming at:

With ES6, You can remove the function keyword and colon altogether when defining functions in objects. Here’s an example of this syntax:

const person = {
  name: "Taylor",
  sayHello() {
    return `Hello! My name is ${this.name}.`;
  }
};

Refactor the function setGear inside the object bicycle to use the shorthand syntax described above.

Compare the example above with the way bicycle.setGear is defined. You’ll have to make yours more like theirs (to match the ES6 style).

I found this one extremely frustrating. The example implies that you should be able to use “setGear()” without passing a variable in the parentheses, but of course you do need to, and then you need to assign it to this.gear.

There has been no previous discussion of the keyword ‘this’; that’s one thing that makes this lesson difficult to complete. I spent some time with https://www.w3schools.com/js/js_this.asp in order to complete this lesson.

2 Likes