Help understading challenge 228 - Making Object Properties

Help understading challenge 228 - Making Object Properties
0.0 0

#1

Hi all!
Could someone help me understand challenge 228 - Making Object Properties Private better? or recommend a site where I can go and understand this subject better?

I tested the code in codepen and the results are different when I do console.lor (myBike.getGear()), yet the challenge was “successful”.

Cheers!

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;
 
  
 this.getGear = function () {
   return gear;
 };
  
 this.setGear = function (num) {
gear = num;
 };

  
  
};

var myCar = new Car();

var myBike = new Bike();

#2

I have a recommendation, read https://github.com/getify/You-Dont-Know-JS, it has great info on objects, this, etc.

the results are different when I do console.lor (myBike.getGear())

possibly because var gear is undefined?

var Bike = function() {

// Only change code below this line.

var gear; //this is not set to anything

this.getGear = function () {
  return gear; //returns what?
};

#3

Thanks for the reply and the link Isaac. I’ll check it out I really did not understand this challenge.

So when I do console.log ( myBike.getGear(1)); the result is 3. And yes, everytime the result is diferent but still a whole number.


#4

so you have

var gear = 1;

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

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

console.log(myBike.getGear());

This should work and return 1, or whatever you set gear to with bike.setGear(var);
CodePen

And why do you have 1 as a parameter in this:

So when I do console.log ( myBike.getGear(1)); the result is 3. And yes, everytime the result is diferent but still a whole number.

getGear should not accept a value, I do not understand what you are trying to do here. If you want to change gear, use setGear. getGear should only return a value not change anything


#5

Okay, before I forget here is a link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

Alright now objects. What you are asking about is more properly called a constructor, which is like a blueprint or factory for making similar objects.

Let’s say we could distill a person down to three essential properties: A name, an age, and an occupation.

So if I said, “hey here’s a person!” You might well respond, “Well what is the person’s name? How old is the person? What is the person’s occupation?”

So how we build that in JavaScript is with a constructor

var Person = function(name,age,occupation){
this.name = name;
this.age = age;
this.occupation = occupation;
};

You can probably see how that’s a blueprint for creating these somewhat limited person objects.

Now we can create a couple of people, Bob and Linda.

var bob = new Person("Bob",24,"Coder");
var linda = new Person("Linda",26,"Better Coder")';

Now we can refer to object properties using dot notation. We can’t just ask “What is your name?” but we can do something like

console.log(bob.name); // logs "Bob"
console.log(linda.occupation); // logs "Better Coder"

Inside the constructor, “this” refers to the object itself. “In this object, the name will be equal to whatever the ‘name’ argument was when the object was constructed.”

I’d break open a text editor such as Visual Studio Code or Sublime Text and practice building objects. The reason is because being able to create, manipulate and iterate through objects and arrays is an absolutely key skill in JavaScript. don’t move on until you’ve got it!

Hope all that helps. Good luck!


#6

Hello i just finished the challenge but i have problem to understand.
`var Bike = function() {

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

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

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

};`

in the method i put on set gear i put gear as an argument and it won’t passed. Then i changed ‘gear’ into ‘num’ on the argument and it passed. I want to know what i did wrong before? anyone could explain to me?


#7

When I got to this exercise I tried out how the example constructor Car really behaves first of all. This helps with the overall understanding of what we are building. Feel free to try my example in codepen here. Just make sure you open the console.


#8

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

I managed to solve this challenge reading the example above and figuring out how it all works. the only thing I am not really clear about is it why in the example the code works when the first function is set “speed +=change” but my code wont run (or at least wont be perceived as correct) if I type the same += and not only =.
Does anybody know why? Thanks!