Make Object Properties Private(what is missing)

Make Object Properties Private(what is missing)
0

#1

Hey guys, can someone please explain to me why this code is not working for this challenge because I just don’t what is wrong with my code

Your code so far

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 speedo = 4;

this.getGear = function() {
  return speedo -= -0;
};  

this.setGear = function() {
  return speedo -= +1;
};  
  

};// End bike fucntion

var myCar = new Car();

var myBike = new Bike();

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.0.11780 Safari/537.36.

Link to the challenge:


#2

Using your code, if I run myBike.setGear(4) it returns 3 - setGear should not return anything. It should set gear to 4, but if I run myBike.getGear() next, I get 3 instead…

Take a look at those two methods: why are you using the -= operator in those methods?


#3


#4

My point was that there is no reason to use the -= operator in those two methods.

getGear should simply return the value of the private variable hidden away within bike. setGear should accept an argument and change the value of the private variable to the value of that argument.


#5

Thanks Faktotum85 :slight_smile:

var Bike = function() {

// Only change code below this line.
var speedo = 4;

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

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

};// End bike fucntion

var myCar = new Car();

var myBike = new Bike(5);


#6

There you go! That looks good to me.