0

Can someone please help me with this method challenge, the discount is not being calculated.

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) { //your code goes here this.discount = discount; return price - (discount * price); } } Values for the first case are... Price : 1700; Discount : 15;

27th Nov 2020, 1:43 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
6 Respostas
+ 2
Challenge has been solved. Here is the solution: 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) { //your code goes here this.discount = discount; this.price = this.price - (this.discount/100 * this.price); } }
27th Nov 2020, 2:38 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
+ 1
Ipang it does not work, answer is incorrect
27th Nov 2020, 2:33 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
+ 1
Ipang we are all learning together. Your answer helped a bit. So thank you for your response
27th Nov 2020, 3:11 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
Try this in changePrice function this.discount = discount; this.price = this.price * ((100 - this.discount) / 100); (Edited - Wrong calculation)
27th Nov 2020, 1:50 PM
Ipang
0
Brandon Banks Sorry I was giving a wrong calculation. Good job with solution 👍
27th Nov 2020, 3:06 PM
Ipang
0
Same solution but maybe this is more neater :)) function main() { var prodID = readLine(); var price = parseInt(readLine(),10); var discount = parseInt(readLine(),10); //call out 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) { this.discount = discount ; //your code goes here this.discount = discount/100; this.price = this.price -(this.discount * this.price) } }
25th Aug 2021, 10:56 AM
riven 2018
riven 2018 - avatar