0

Methods and functions in JavaScript

Please can someone help me with this code Need fix the method in calculating the discount 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.price); } function Product(prodID, price) { this.prodID = prodID; this.price = price; this.changePrice = function(discount) { // The answer to this code goes here };

23rd Aug 2023, 6:59 PM
Reuben Osei
Reuben Osei - avatar
1 ответ
0
This.price = this.price -(this.price * discount) This is known as a mutator, as it changes the underlying code, a more sustainable way to keep the original prices would be this Return this.price -(this.price * discount) Then there would be no underlying data manipulation, then if you wanted to you could create a set price method. also, this is hypothetical, I don't remember all the JavaScript syntax, so you will have to appropriately apply what I said to JavaScript rules
23rd Aug 2023, 7:39 PM
StealthyTaco
StealthyTaco - avatar