Make Object Properties Private
I am not able to find a bug in this snippet.
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;
this.setGear=function(set){
return set;
};
this.getGear=function(){
gear += set;
};
};
var myCar = new Car();
var myBike = new Bike();
Hi @debashispanda! After looking over the challenge along with your code, I noticed that the issue is with the code you are submitting and not with the challenge itself.
You seem to be mixing up the purposes of the setGear(set) and getGear() functions. Remember, getter functions are meant to just return (get) the value of a private variable, and setter functions are meant to change (set) the value of a private variable to a different value. Take a look at the tests for the challenge to see examples of how the functions are expected to work. If you need any more help, feel free to ask the community in this chat room for assistance. Happy coding!
Hi @debashispanda, as pointed by @robbawebba your code is incorrect.
Thanks for the report. The issue tracker is for reporting bugs only.
Please use the chat rooms:
or try looking through our forum for help with a specific challenge.
Happy Coding!
I got it and i made the changes...thank you for your help
On 5 Jan 2017 9:45 p.m., "Rob Weber" notifications@github.com wrote:
Hi @debashispanda https://github.com/debashispanda! After looking over
the challenge along with your code, I noticed that the issue is with the
code you are submitting and not with the challenge itself.You seem to be mixing up the purposes of the setGear(set) and getGear()
functions. Remember, getter functions are meant to just return (get)
the value of a private variable, and setter functions are meant to
change (set) the value of a private variable to a different value. Take
a look at the tests for the challenge to see examples of how the functions
are expected to work. If you need any more help, feel free to ask the
community in this chat room
https://gitter.im/FreeCodeCamp/HelpJavaScript for assistance. Happy
coding!—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/FreeCodeCamp/FreeCodeCamp/issues/12385#issuecomment-270683350,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AXcwWVsgLUO53s5PhYfquPAAhxoznLVpks5rPRcGgaJpZM4Lbz5U
.
Hello, I would like an explanation about this challenge. If you take a look at the accelerate property of the example, you can see that it is using the "+=" to add to change the value of the speed variable. However when I do this same with my "setGear" property, It does't pass the challenge. The only way it will pass the test is by removing the "+".. Can you please explain why it doesn't work with the "+" operator?
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;
this.setGear = function(val){
gear += val ;
};
this.getGear = function(){
return gear;
};
};
var myBike = new Bike();
myBike.setGear(3);
Hi @qkoots, thank you for your report. I'll message you on gitter to explain the challenge a bit more. Please keep in mind that the GitHub issue tracker is for bugs only. If you need more help with the challenges, please use the chat rooms:
JavaScript help chat room
Other Official Rooms
Thanks, and happy coding!
I'm going to add to this because it is confusing as the exercise refuses to validate claiming the test results are not correct, even though they actually are, even with the "+=" instead of the "=".
The exercise keeps calling this as an error:
myBike.getGear() should return 1 after calling myBike.setGear(1).
Even though it does return a 1. (as well as a 3 and 4 when given as argument).
See screenshot
