0

Need help in calculating percentage in JavaScript?

Time to go shopping! Everything in the store has been discounted by 20%. You are given a program that takes the price of an item as input. Complete the program so that it outputs the discounted price to the console. Sample Input 100 Sample Output 80 Explanation 20 percent of 100 equals to 20 (100 * 20/100), so the discounted price will be 80 (100 - 20). what's wrong in my code? //so we don’t overwhelm you, we’ve hidden the code that executes the input function main() { var oldPrice = parseInt(readLine(), 10) var percent = 20/100 * oldPrice ; console.log(percent ) }

28th Jan 2021, 6:16 AM
Bilal Ahmad
Bilal Ahmad - avatar
6 Respuestas
+ 2
Bilal Ahmad You are trying to output the reduced price rather than the discounted price ... so it should be like : after input , var x=20/100*oldPrice; // 20 console.log(oldPrice-x); //like in the eg:100-20
28th Jan 2021, 6:48 AM
Alphin K Sajan
Alphin K Sajan - avatar
0
Post your attempt first to get help from the community... Edit : Question edited ...
28th Jan 2021, 6:19 AM
Alphin K Sajan
Alphin K Sajan - avatar
0
thanx buddy
28th Jan 2021, 6:53 AM
Bilal Ahmad
Bilal Ahmad - avatar
0
Wlcm :)
28th Jan 2021, 9:04 AM
Alphin K Sajan
Alphin K Sajan - avatar
0
// your code goes here var x=20/100*oldPrice; // 20 console.log(oldPrice-x);
7th May 2021, 1:14 PM
Alharith Yousif
Alharith Yousif - avatar
- 2
added my attempt
28th Jan 2021, 6:29 AM
Bilal Ahmad
Bilal Ahmad - avatar