freeCodeCamp Challenge Guide: Make Object Properties Private

freeCodeCamp Challenge Guide: Make Object Properties Private
0.0 0

#1

Objects have their own attributes, called properties, and their own functions, called methods.

You can use the this keyword to reference public properties and methods of the current objects. However, when You need to create private ones so they are not accessible from the outside of the object you just remove the keyword this from the object property or method declaration and declare it with var so that it is private outside its scope.

var Bike = function() {
  var speed = 100; // private
  function addUnit(value) { // private
    return value + "KM/H";
  }

  this.getSpeed = function () {  // public
    return addUnit(speed);
  };

};

Another example:

var Cake = function() {

  var loot = 2;
  // Getter to know how much loot you have
  this.getLoot = function() {
    return loot;
  };

  // Setter to change the ammount of loot
  this.setLoot = function(num){
    loot = num;
  };

Can someone explain the use of num in this code
#2

Hi,
My code seems to run properly but it doesn’t pass the test. I have looked at everything but I can’t pass the test for myBike.getGear(); despite it clearly showing that gear =4 when myBike.setGear(4);
What am I doing wrong?

var Bike = function() {
  var gear=0;
  
  this.setGear = function(addGear){
    gear += addGear;   
  };
  this.getGear = function(){
    return "Gear "+ gear;    
  };

};
var myBike = new Bike();
myBike.setGear(8);

#3

dotkok when you call myBike.setGear() you are actually doing sum.
try calling at the end of your code myBike.setGear(4); then myBike.setGear(3);. You will see the result is 7.
The problem is in how you use your gear variable. Take a look at this line of code. gear += addGear;
This dose not allow the gear variable to reset to it’s initial value (0) after each call by the method.

Solution:

gear = addGear;

Hope it helped you ;).


#4

Thank you so much for your reply.
I have tried your suggestion but I still do not seem to pass the test.(see pic below)

Here is the code with your correction.
var Bike = function() {

  var gear=0;
  
  
  this.setGear = function(addGear){
    gear = addGear;   
  };
  this.getGear = function(){
    return "Gear "+ gear; 
    
  };

};

var myBike = new Bike();
myBike.setGear(5);
myBike.setGear(3);

#5

The reason that is not working is because you are returning a string together with the answer in the statement - return "Gear " + gear;

Try removing the “Gear” and leave only the variable gear and it should work.


#6

Thank you so much!! I combined both of your corrections and it worked! Thanks!! @leizifei @vicarmocanu


#7

var Bike = function() {
var gear = 10;
this.setGear = function(x){
gear = x;
};
this.getGear = function(){
return gear;
};
// Only change code below this line.

};

myBike.setGear(4);


#8

This is the code that worked for me. I think it’s important that you don’t define gear.
var Bike = function() {

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

};

};

var myBike = new Bike();


#9
var Bike = function() {
  var gear = 10;

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

var myCar = new Car();

var myBike = new Bike();

#10

Nice I was doing the same thing!!! Thanks


#11

is location of this.setgear and this.getgear matters?

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

is above code different than below one

var Bike = function() {

  // Only change code below this line.
  
  var gear;

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

both above code passes test.
even I put “var gear” at the end of object it passes test, why?


#12

The location of the methods do not matter. the order in which the methods are invoked matter which in this case is setGear first and then getGear.

Just declaring var gear means that its value is initially undefined. when setGear is called, gear is set to whatever was passed in as an argument. so it does not matter in this case if gear was set to an initial value or not


#13

This also worked:

var Bike = function() {

// Only change code below this line.
var gear = 0;

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

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

};

};

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(4);


#14

Hello campers,this is how I approached it:

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 = 5;//private variable

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

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

};

var myCar = new Car();
myCar.getSpeed();

var myBike = new Bike();
myBike.getGear();


#15

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.getGear=function (change){
change= gear;
} ;

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

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(1);
myBike.setGear(3);
myBike.setGear(4);

Hi there, i am having issue with this challenge, i cant seem to figure this out


#16

AGAIN!!! Another example of NOT teaching us what we need to know to solve the problems.
I understand I can google stuff. But some of the exercises are very specific.

You CANNOT solve this problem without knowing that you need Num.

this.setLoot = function(num){
loot = num;
};

But you don’t learn that this is even a parameter that need to be passed until you look in the Hints.

PLEASE, PLEASE, PLEASE. I want to learn this stuff through practice, not by getting the answers.


#17

I don’t get any error message with this code. Could someone point out what I am doing wrong? Thanks!

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

};

var myCar = new Car();

var myBike = new Bike();
myBike.getGear(4);

EDIT: Figured it out.

It says set.Gear should be called - not get.Gear.

THIS WORKS:
var Bike = function() {
var gear = 0;

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

this.setGear = function(num) {
gear = num;

};

};

var myCar = new Car();

var myBike = new Bike();
myBike.setGear(4);


#18

This worked for me