Why say discount is not defined? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why say discount is not defined?

In practice 34.2 I get the error that discount is not defined, why? function main() { var prodID = readLine(); var price = parseInt(readLine(),10); var discount = parseInt(readLine(),10); var prod1= new Product(prodID, price); console.log(prod1.prodID + " price: " + prod1.price); prod1.changePrice(discount); console.log(prod1.prodID + " new price: " + prod1.changePrice); } function Product(prodID, price) { this.prodID = prodID; this.price = price; this.discount = discount; this.changePrice = function(){ return this.price - (this.price * (this.discount / 100)) } };

14th May 2021, 7:32 PM
Richard
Richard - avatar
2 Answers
+ 1
discount is only defined in main, not in Product. This priciple is called scope
14th May 2021, 7:49 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
You have to define discount outside the main function because it's locally defined in main and the product function has no access to this variable.
14th May 2021, 8:08 PM
Philipp Makarov
Philipp Makarov - avatar