0

JavaScript Code Problem

This is one of the SoloLearn JavaScript code challenges. The goal of the problem is given three inputs (product ID, price and discount), I need to display the current price and the new discounted price. I have accomplished the goal, but 1 out of 5 tests fail and it is consistently the 4th test. Unfortunately I cannot see the input, expected output and actual output, so I have no idea why. I have tried adding Math.round() to the code, but it produces the same results. I appreciate any and all help. Thank you! process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.replace(/\s*$/, '') .split('\n') .map(str => str.replace(/\s*$/, '')); main(); }); function readLine() { return inputString[currentLine++]; } 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); //EVERYTHING ABOVE THIS WAS GIVEN IN THE PROBLEM AND IS UNCHANGED. My code is below 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.price = price - (price * discount / 100); } };

13th Oct 2020, 2:38 PM
Richard Villarreal
Richard Villarreal - avatar
5 Réponses
0
Try.. Number((the result number).toFixed(2)).. maybe it's persicion issue ..rounding to the second decimal can do it
16th Oct 2020, 6:54 AM
Ali Kh
Ali Kh - avatar
0
Thank you for the response! Unfortunately this did not fix the issue. The expected output for the three cases that I can see is rounded to the nearest integer. I tried your suggestion with .toFixed(0) and it did not fix the one failing test either.
16th Oct 2020, 7:43 AM
Richard Villarreal
Richard Villarreal - avatar
0
In the function 'changePrice' how did the 4 tests even work even though you didn't bindd the 'price' value to the object instance .. I mean the equation in the function, shouldn't it be like this : this.price = this.price - (this.price * discount /100); Don't mean to confuse you, but try this out
16th Oct 2020, 7:50 AM
Ali Kh
Ali Kh - avatar
0
I’m guessing it worked because the price variable was defined in the main function. I will try that now
16th Oct 2020, 10:42 AM
Richard Villarreal
Richard Villarreal - avatar
0
Unfortunately that had the same result as the previous: 4/5 tests pass
16th Oct 2020, 10:43 AM
Richard Villarreal
Richard Villarreal - avatar