Just like in the example given, rather than declaring the weight variable with the this keyword, the let keyword must be used to declare it as a private variable. This way it can only be accessed inside the Bird function. The getWeight method must then be added inside the Bird function to access the weight variable.
Solutions
Solution 1 (Click to Show/Hide)
function Bird() {
let weight = 15;
this.getWeight = function() {
return weight;
};
}
Solution 1 (Click to Show/Hide)
In ES6 syntax we can make the function a bit less verbose:
function Bird() {
let weight = 15;
this.getWeight = () => weight;
}
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);
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.
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();
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?
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
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.
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.