I wonder what could I change in this code? Only these seem to be right:
The method getGear of myBike should be accessible outside the object.
The method setGear of myBike should be accessible outside the object.
myBike.gear should remain undefined.
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.
for (var gear = 0; gear < 15; gear++);
this.getGear = function() {
return gear;
};
this.setGear = function() {
return gear;
};
};
var myCar = new Car();
var myBike = new Bike();
var Bike = function() {
// Only change code below this line.
for (var gear = 0; gear < 15; gear++);
this.getGear = function() {
return gear;
};
this.setGear = function() {
return gear;
};
};
In this code you only define for 15 times your getGear and setGear methods, and if you define var gear inside a for the visibility of gear is only inside the for, you have to define it outside the block.
( Another question: why you do for ( var gear = 0; gear < 15; gear++ ) ? )
I did the for ( var gear = 0; gear < 15; gear++ ) so that it would have some range on which to adjust the gear.
Now I changed it into this, but these are still wrong:
myBike.getGear() should return 4 after calling myBike.setGear(4).
myBike.getGear() should return 3 after calling myBike.setGear(3).
myBike.getGear() should return 1 after calling myBike.setGear(1).
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.getGear = function() {
return gear;
};
this.setGear = function() {
return gear;
};
};
var myCar = new Car();
var myBike = new Bike();
You’re close, but mixing up getGear and setGear… I did the exact same thing! I’ve explained the corrected code below…
Bike = function() {
// Step 1: Set "var gear" as shown below. I noticed you assigned it a value of "0", but the challenge states to leave it undefined.
var gear;
// Step 2: The setGear method should "set" the gear equal to whatever the input is. In this case it's "change".
this.setGear = function(change) {
gear = change;
};
// Step 3: The getGear method should "get" the gear, so you basically just ask it to return "gear".
this.getGear = function() {
return gear;
};
};
var myCar = new Car();
var myBike = new Bike();
You’re setting the value of gear to the current value + the value of a. Every time you call your setGear function the value of gear will keep increasing. All you need to do is to set the value of gear to the value of a.
First, I’ve noticed you set the gear to 0. The lesson stays to leave it undefined. When coding this in my own text editor, setting it to 0 or leaving it undefined worked either way, but since FreeCodeCamp’s editor looks for this requirement, you have to type what they’re looking for.
The other thing I notice in your code is that you’re incrementing the gear by adding the input to it. “gear+a”. This code could work to a degree. Obviously, you would have to set parameters since you can’t add and get to the 10th gear. No such gear exists on a car! That being said, while you could code it this way it’s doing much more than what the challenge is asking. It’s literally asking you to “tell” it what gear to be in. No addition or subtraction necessary! I hope this helps!
You’re trying to access a variable in the function without initializing it inside the function.
Those errors should be detected quickly as they can be hard to detect afterwards. If you’re having problems detecting those you can try using a program to help you. I tend to use chekcmarx these days which brings some pretty good results.
Good luck!
Michael.
@aniellocristo Thanks so much that was a very clear explanation as well as nicely presented so even I understood ; )
Not to be picky but shouldn’t getGear come before setGear based on their challenge?
var Bike = function() {
var a;
this.setGear = function (b){
a = b;
};
this.getGear = function (){
return a;
};
// Only change code below this line.
};
var myCar = new Car();
var myBike = new Bike();