[SOLVED] Stuck in Make Object Properties Private challenge

[SOLVED] Stuck in Make Object Properties Private challenge
0.0 0

#1

I’m stuck on this challenge, and it’s frustrating. I tried to tweak my code many times without success. I passed 3 out of the 6 requirement and I don’t know what to do next. Any help is very much appreciated.
here is my code:

var Car = function() {
// this is a private variable
var speed = 10;

// these are public methods
this.accelerate = function(change) {
speed += change;
};

this.decelerate = function() {
speed -= 5;
};

this.getSpeed = function() {
return speed;
};
};

var Bike = function() {

// Only change code below this line.
var gear;

this.setGear = function(gear){
return gear;
};

this.getGear = function(setGear){
return setGear;
};
};

var myCar = new Car();

var myBike = new Bike();


Thank you


#2

Neither of your functions are doing anything other than returning the arguments they are passed.

Presumably you want setGear to take the passed value and assign it to gear. But you’re not assigning anything anywhere. If you create a function that takes an argument with the same name as another variable that is within the scope of the function, the function will be blind to the other variable. It will only see the argument by that name.

You need to rename your argument to something else. Then you need to take that argument value and assign it to gear.

Your getGear function doesn’t need any parameters at all. It’s job is to expose the value of gear. Right now it takes a value and simply returns that value. It needs to return the current value of gear.


#3

I finally figured it out. Thank you michealhall for your help.
Here is the source code for anyone that needs help with this challenge in the future:

var Car = function() {
// this is a private variable
var speed = 10;

// these are public methods
this.accelerate = function(change) {
speed += change;
};

this.decelerate = function() {
speed -= 5;
};

this.getSpeed = function() {
return speed;
};
};

var Bike = function() {

// Only change code below this line.
var gear;

this.setGear = function(num){
gear = num;
};

this.getGear = function(){
return gear;
};
};

var myCar = new Car();

var myBike = new Bike();

//test
myBike.setGear(4);
console.log(myBike.getGear());