Make Object Properties Private...What's wrong?

Make Object Properties Private...What's wrong?
0

#1

Hi,

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();

#2
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++ ) ? )


#3

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();

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

To set a new value your function need an argument ( the new value ) and then set gear to the new value.

function (new_value) { gear = new_value; }


#5

@CoderInFreeCodeCamp I think what this challenge is trying to teach is “closure” in JS. I found a related video here: https://www.youtube.com/watch?v=yiEeiMN2Khs


#6

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(); 

I hope this helps!


#7

Thank you @aniellocristo :slight_smile: Very well explained. I finally got this done right :slight_smile:


#8

awesome. great operation!


#9

Thanks @aniellocristo that was really helpful. I first tried something very similar, but failed. What am I missing here? :

var Bike = function(a) {

// Only change code below this line.
var gear=0;
this.setGear = function(a){
gear=gear+a;
};
this.getGear = function(){
return gear;
};
};

Regards


#10

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.


#11

i got now really crazy

if every thing is undefined and unknown , then how it know to + it…
you didn’t say if … then do this ++ or some thing like that

can you please explain it


#12

@JoGarCab, sorry for the late response!

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!


#13

@tareq4 You, actually aren’t incrementing it. You’re replacing the value with whatever is passed into the function.

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

So, if I type in 4 for the change value, it’s like telling the computer to forget “undefined” and replace it with 4.


#14

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.


#15

@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?


#16

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() {
var gear;

this.setGear= function(val){
this.val1=val;

};

this.getGear =function(gear){

return this.val1;

};
// Only change code below this line.

};

var myCar = new Car();

var myBike = new Bike();

Complete running solution


#17

This my help to understand

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();


#18

Thanks. it helps and works well.


#20

Great video, thank you!