The else if statement - JS - practice 16.2 - Buy More, Get More | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The else if statement - JS - practice 16.2 - Buy More, Get More

can you please check what am I doing wrong...? function main() { var totalPrice = parseInt(readLine(), 10) var x = totalPrice; if (x >= 5000) { console.log("50%"); } else (x <= 4999 && x >= 3000) { console.log("30%"); }

26th Aug 2021, 6:32 PM
Pawel
6 Answers
+ 2
Syntax error Try to write : else if not only else
26th Aug 2021, 8:18 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
Pawel , post the full description of the task, also example input and output if available, so somebody can help you.
26th Aug 2021, 7:26 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Pawel , else clause can be used of course, but not that way => else means in any other case, so it doesn't contain condition. According to your code it can be changed to: CODE: function main() { var totalPrice = parseInt(readLine(), 10) var x = totalPrice; if (x >= 5000) { console.log("50%"); } else if (x <= 4999 && x >= 3000) { console.log("30%"); } else if (x <= 2999 && x >= 1000) { console.log("10%"); } else { console.log("0%"); }
27th Aug 2021, 12:43 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Hey else is like default case Not a else if statement
27th Aug 2021, 9:50 AM
Vtec Fan
Vtec Fan - avatar
0
@Muhammad ("else if" - still not working...) @TheWhiteCat: Problem description: 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% MY CODE: function main() { var totalPrice = parseInt(readLine(), 10) var x = totalPrice; if (x >= 5000) { console.log("50%"); } else if (x <= 4999 && x >= 3000) { console.log("30%"); } else if (x <= 2999 && x >= 1000) { console.log("10%"); } else (x <= 999 && x >= 1) { console.log("0%"); }
27th Aug 2021, 10:52 AM
Pawel
0
OK it worked, but I still don't guite get, why we can't use "else if" and "else" in here. function main() { var totalPrice = parseInt(readLine(), 10) var x = totalPrice; if (x > 5000) { console.log("50%"); } if (x < 4999 && x > 3000) { console.log("30%"); } if (x < 2999 && x > 1000) { console.log("10%"); } if (x < 999) { console.log("0%"); } }
27th Aug 2021, 11:41 AM
Pawel