+ 1

Trying to solve calculating the discount in JavaScript

A store manager needs a program to set discounts for products. The program should take the product ID, price and discount as input and output the discounted price. However, the changePrice method, which should calculate the discount, is incomplete. Fix it! Sample Input LD1493 1700 15 Sample Output LD1493 price: 1700 LD1493 new price: 1445function 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.price = price; } } its ouputting the same price

18th May 2021, 4:21 PM
Simisola Osinowo
Simisola Osinowo - avatar
7 Respuestas
+ 5
this is a complete 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.price =this.price - this.price * discount/100; } }
27th Aug 2021, 3:08 AM
Dami
Dami - avatar
+ 1
Share more detail than just saying it does not work. Share any error messages. Share the output. If it just printed the same price before, it is hard to imagine this not working. Below is code that prints out: LD1493 price: 1700 LD1493 new price: 1685 I tested it using Sololearn's Code Playground nodejs feature. I'm not sure where your readLine function is coming from so I simulated it. I also added a call to main() so it would actually run. var lines = [ 'LD1493', '1700', '15' ]; var lineIndex = 0; function readLine() { return lines[lineIndex++]; } 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.price = this.price - discount; } } main();
19th May 2021, 6:51 PM
Josh Greig
Josh Greig - avatar
+ 1
do this !! this.price = price - (price * discount / 100);
30th May 2021, 7:12 PM
JRAMAHES
JRAMAHES - avatar
0
Have you tried replacing your definition of changePrice with this? this.changePrice = function(discount) { //your code goes here this.price = this.price - discount; }
18th May 2021, 5:31 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig it still did not work
19th May 2021, 3:16 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
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.discount); } function Product(prodID, price) { this.prodID = prodID; this.price = price; this.changePrice = function(discount) { this.discount =this.price - this.price * discount/100; } }
15th Nov 2023, 4:36 PM
Dulanji Algama
Dulanji Algama - avatar
0
i stuck in this exercise. I have not any output. I tried anything. Is this broken on what???
31st Oct 2024, 4:28 PM
Patryk Skonieczny
Patryk Skonieczny - avatar