I'm doing a simple constructor exercise and I need to change a value after the initial creation of the constructor. When I run it the cat is still not making its noise after I changed its value to true.
The constructor function will take in two parameters, raining and noise. These will be passed into the keys as their value.
Create a third key that will be a function called makeNoise(). The function checks if the raining key's value is true. If it is, it will log the value of the key's noise in the console.
Bonus
raining for the cat object after it has been created?function Animal(raining, noise) {
this.raining = raining,
this.noise = noise;
this.makeNoise = function(){
if(this.raining)
console.log(noise)
}
}
// Creates `dog` and `cat` objects with `raining` and `noise` properties
let dog = new Animal(true, 'Woof!');
let cat = new Animal(false, 'Meow!');
// Calls the `makeNoise()` methods on the `dog` and `cat` objects
dog.makeNoise();
cat.makeNoise();
// BONUS CODE HERE
cat.raining= true;
You have to call makeNoise() after setting the raining property to true. When you call makeNoise() before, the raining property is false (since JS is (mostly) executed sequentially), so nothing is logged.
function Animal(raining, noise) {
this.raining = raining,
this.noise = noise;
this.makeNoise = function() {
if (this.raining)
console.log(noise)
}
}
// Creates `dog` and `cat` objects with `raining` and `noise` properties
let dog = new Animal(true, 'Woof!');
let cat = new Animal(false, 'Meow!');
// Calls the `makeNoise()` methods on the `dog` and `cat` objects
dog.makeNoise();
cat.makeNoise();
// BONUS CODE HERE
cat.raining = true;
cat.makeNoise();