I am currently working on a simple constructor exercise where I need to update a value after the constructor has been created. Even after changing the value to true, the cat is still not making its noise.
Challenge parameters
The constructor function will accept two parameters,
raining
andnoise
. These parameters will be assigned as values to keys within the constructor function.Add a third key named
makeNoise()
which is a function. This function checks if the value of theraining
key is set totrue
. If it is, then it logs thenoise
key's value in the console.
Bonus
- How can we make the cat produce noise? In other words, how do we change the value of
raining
for thecat
object after it has already been created?
function Animal(raining, noise) {
this.raining = raining,
this.noise = noise;
this.makeNoise = function(){
if(this.raining)
console.log(noise)
}
}
// Creating `dog` and `cat` objects with respective `raining` and `noise` properties
let dog = new Animal(true, 'Woof!');
let cat = new Animal(false, 'Meow!');
// Calling the `makeNoise()` methods on both the `dog` and `cat` objects
dog.makeNoise();
cat.makeNoise();
// BONUS CODE HERE
cat.raining= true;