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%"); }
6 Respuestas
+ 2
Syntax error
Try to write : else if not only else
+ 1
Pawel , post the full description of the task, also example input and output if available, so somebody can help you.
+ 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%");
}
0
Hey else is like default case
Not a else if statement
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%");
}
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%");
}
}