Freecodecamp: Make Object Properties Private , returns correct in console but doesn't pass challenge?

Created on 6 Mar 2016  路  12Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Make Object Properties Private has an issue.
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

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 = 0;

  //public methods
  this.setGear= function(change) {
    gear += change;
  };

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

};

var myCar = new Car();

var myBike = new Bike();

myBike.setGear(4);
myBike.getGear();

Most helpful comment

ohh right! Thank you it works now, changed:
gear += change; to
gear = change;

All 12 comments

As you can see i've tried testing to set the value 4 using setGear(4) and getGear returns 4. However the activity isn't passed.

fcc

Check out what happens if you call the setGear method twice. Next time please visit the Help Chat to confirm what you're seeing is a site issue and not an issue with your solution before filing a Github Issue. Thanks and happy coding!

ohh right! Thank you it works now, changed:
gear += change; to
gear = change;

this helped me as well :+1:

This helped me too! Thank you!

this also helped me but I have a point to raise. In my case why "+=" issue was working for setGear(4) and not for setGear(3)/setGear(1);

wow...I am finally through..Thank you!

thanks, but why "+=" cant complete it?

I really don't understand why its not working out with +=. Maybe they did not want us to just copy the example. or is the code running more than one time so the second time gear will be added to the old value? with +=? thats maybe it but im getting the right answers with += in my console so I does not make any sense...

I think the += is performing an addition to the gear instead of just displaying it.

I found this example on GitHub and it works;
removed solution by mod

@Gichunge
Please do not post the solution on here. GitHub Issues are for reporting bugs on the website only.

Happy Coding!

Was this page helpful?
0 / 5 - 0 ratings