How to solve this task "Buy More, Get More"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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%

4th Jan 2021, 12:28 AM
Sergey Zapara
Sergey Zapara - avatar
9 Answers
+ 13
You missed totalpPrice in last two conditions
4th Jan 2021, 1:25 AM
Simba
Simba - avatar
+ 5
Also the last condition should be `else if`, `else` does not accept conditions.
4th Jan 2021, 2:17 AM
Ipang
+ 3
Thanks for the help 😉👍
4th Jan 2021, 7:13 AM
Sergey Zapara
Sergey Zapara - avatar
+ 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); }
17th Jun 2021, 10:01 AM
Agon Istrefi
+ 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.
15th Jan 2021, 10:22 PM
Martijn Greven
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%') } }
4th Jan 2021, 12:30 AM
Sergey Zapara
Sergey Zapara - avatar
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%")
9th Jun 2021, 11:55 AM
Chukwuemeka Okore
Chukwuemeka Okore - avatar
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);}
25th Aug 2021, 2:02 AM
MD RAKIBUL HASSAN NAYON
MD RAKIBUL HASSAN NAYON - avatar
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
25th Jan 2022, 8:17 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar