I have a question relating with using if statement within my prototype constructor.
What I am trying to do:
- Create a method calculatePrice for the Item constructor.
- This method will return the price of the object by default.
- Item is fruit, return the price of the Item minus 5%.
Code ** NOT WORKING
function Item(name, price){
this.name = name;
this.price = price;
}
Item.prototype.calculatePrice = function() {
if (this.name === 'fruit') {
this.price = this.price * 0.95
} else {
this.price = this.price;
}
}
var ball = new Item('soccer ball', 15);
ball.calculatePrice();
// Expected results: 15
var fruit = new Item('fruit', 10);
fruit.calculatePrice();
// Expected results: 9.5
However my error is how I wrote the if statement. Without giving me the solution, could you please direct me on where my mistake was made? Thank you.