- 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%
9 Answers
+ 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%")
}*/
+ 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 ;)
+ 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 ^^
+ 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) ^^
+ 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!
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%");}}
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??
0
Thank you both that solved it ^^
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%");
}
}