0
How to solve this task "Buy More, Get More"?
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
+ 13
You missed totalpPrice in last two conditions
+ 5
Also the last condition should be `else if`, `else` does not accept conditions.
+ 3
Thanks for the help đđ
+ 2
function main() {
var totalPrice = parseInt(readLine(), 10);
var discount;
// Your code here
if (totalPrice >= 5000){
discount = '50%'
} else if ( totalPrice >= 3000 && totalPrice < 5000) {
discount = '30%'
} else if (totalPrice >= 1000 && totalPrice < 3000) {
discount = '10%'
} else {
discount = '0%'
}
console.log(discount);
}
+ 1
this code is also working can some one explaine to why they use the && ?
function main() {
var totalPrice = parseInt(readLine(), 10)
// Your code here
if (totalPrice <= 999)
{console.log("0%")}
else if (totalPrice <=2999)
{console.log("10%")}
else if (totalPrice<=4999)
{console.log("30%")}
else{console.log("50%")}
}
i know the progame is used to run it form top to bottem. so this way there is no needt for the && extra code.
0
Here is the final solution:
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%')
}
}
0
This is a more shorter line involving all the if, else if and else statements.
if(totalPrice <= "999" && "0")
{console.log("0%")}
else if(totalPrice <= "2999" && "1000")
{console.log("10%")}
else if(totalPrice <= "4999" && "3000")
{console.log("30%")}
else
console.log("50%")
0
function main() {
var totalPrice = parseInt(readLine(), 10)
// Your code here
var discount ;
if (totalPrice >=5000){
discount = "50%"
}
else if (totalPrice>=3000 && totalPrice<=4999)
{
discount = "30%"
}
else if (totalPrice>=1000 && totalPrice<=2999){
discount = "10%"}
else {
discount = "0%"
}
console.log(discount);}
0
function main() {
var totalPrice = parseInt(readLine(), 10);
var discount;
// Your code here
if (totalPrice >= 5000){
discount = '50%'
} else if ( totalPrice >= 3000 && totalPrice < 5000) {
discount = '30%'
} else if (totalPrice >= 1000 && totalPrice < 3000) {
discount = '10%'
} else {
discount = '0%'
}
console.log(discount);
}
Good Luck