- 1

Help with exercise 16.2 Javascript

Hi guys! I've been trying to solve this exercise for a while, I must be missing something, any feedback is really appreciated 😊 A store is offering a tiered discounts based on the purchase total. 5000 and above => 50% 3000-4999 => 30% 1000-2999 => 10% 1-999 => 0% Write a program that takes the total price as input and outputs the corresponding discount to the console. Sample Input 4700 Sample Output 30%

18th Mar 2021, 12:56 PM
Nemezio Lopezperez
Nemezio Lopezperez - avatar
9 Respostas
+ 7
You need to use else if instead of else in your last condition or you can use else without any condition. else if (totalPrice >= 1 && totalPrice <= 999){console.log("0%");}} /*or else{ console.log("0%") }*/
18th Mar 2021, 1:27 PM
Simba
Simba - avatar
+ 3
you must use console.log() to output result... (task is executed in nodejs environment, wich doesn't provide document object (so doesn't provide document.write)... check if this is enough to solve your problem ;)
18th Mar 2021, 1:03 PM
visph
visph - avatar
+ 3
yeah, Simba is right: I did not accuratly read your code as I missunderstanding your last post: I was thinking the problem was solved ^^
18th Mar 2021, 1:30 PM
visph
visph - avatar
+ 2
in nodejs, as already said, there's no document nor document.write... so you get an error (but I guess you only see 'no output' as result) ^^
18th Mar 2021, 1:23 PM
visph
visph - avatar
+ 1
I was looking for some explanation Why “document.write” and “alert” don’t work, and this topic solved the mystery, thanks visph for the explanation!
28th Jun 2021, 1:10 AM
Álison Freire
0
//This is the code I wrote function main() { var totalPrice = parseInt(readLine(), 10) // Your code here if (totalPrice >= 5000){document.write("50%");} else if (totalPrice >= 3000 && totalPrice <= 4999) {document.write("30%");} else if (totalPrice >= 1000 && totalPrice <= 2999){document.write("10%");} else (totalPrice >= 1 && totalPrice <= 999){document.write("0%");}}
18th Mar 2021, 12:57 PM
Nemezio Lopezperez
Nemezio Lopezperez - avatar
0
Hey Visph! Thank you, I've tried it out but still not working :/ I've been at it for 2 days wonder what is wrong??
18th Mar 2021, 1:06 PM
Nemezio Lopezperez
Nemezio Lopezperez - avatar
0
Thank you both that solved it ^^
18th Mar 2021, 1:31 PM
Nemezio Lopezperez
Nemezio Lopezperez - avatar
0
function main() { var totalPrice = parseInt(readLine(), 10) // ĐČĐČДЎОтД ĐșĐŸĐŽ сюЮа if(totalPrice >= 5000){ console.log("50%"); }else if(totalPrice >= 3000 && totalPrice <= 4999){ console.log("30%"); }else if(totalPrice >= 1000 && totalPrice <= 2999){ console.log("10%"); }else if (totalPrice >= 1 && totalPrice <= 999){ console.log("0%"); } }
12th Sep 2021, 12:21 PM
Yan Zinchenko
Yan Zinchenko - avatar