- 1

Buy more, get more, if..else if...

I tried my code in code pen and it seemed to work but the app is throwing me an error. Can any one help please function main() { var totalPrice = parseInt(readLine(), 10) // Your code here if (totalPrice <= 4999 && totalPrice >= 3000) { document.write('30%'); } else if (totalPrice <=2999 && totalPrice >= 1000) { document.write('10%'); } else if (totalPrice <= 999 && totalPrice >= 1) { document.write('0%'); } else { document.write('50%'); } Code pen link: codepen.io/Leanne251/pen/ZELEzwG

22nd Mar 2021, 7:37 PM
Leanne Smith
Leanne Smith - avatar
12 Antworten
+ 2
sure: at least in your copy of code here, the closing curly bracket of the main function is missing ^^
22nd Mar 2021, 9:29 PM
visph
visph - avatar
+ 1
Thanks for your help!! I popped in the missing bracket and changed to console.log and its worked now. Please could you let me know if the if statment should be in the function like this... and if i have any syntax missing, e.g should there be a ; on amd of the }, especially the ones at the end? function main() { var totalPrice = parseInt(readLine(), 10) // Your code here if (totalPrice <= 4999 && totalPrice >= 3000) { console.log('30%'); } else if (totalPrice <= 2999 && totalPrice >=1000) { console.log('10%'); } else if (totalPrice <= 999 && totalPrice >=1) { console.log('0%'); } else { console.log('50%'); } }
23rd Mar 2021, 8:34 AM
Leanne Smith
Leanne Smith - avatar
+ 1
visph I mean only "If you forgot adding semicolon , it won't give " error ", if statements are in different lines in non-strict mode". Actually i missed there adding of 'missing semicolon' instead of 'semicolon' just. I edited it.. thank you..
23rd Mar 2021, 9:27 PM
Jayakrishna 🇮🇳
+ 1
Thanks 😁😁
23rd Mar 2021, 10:54 PM
Leanne Smith
Leanne Smith - avatar
0
It wont let me paste the screen shot of the error but it says syntax error Unexpected end of input.
22nd Mar 2021, 7:42 PM
Leanne Smith
Leanne Smith - avatar
0
Try console.log() instead of document.write();
22nd Mar 2021, 8:02 PM
Jayakrishna 🇮🇳
0
Codepen and posted snippet are diferent, as visph tells you the closing curly bracket of the main function is missing ^^ Why you are trying to wrap all inside function main?
23rd Mar 2021, 1:51 AM
David Ordás
David Ordás - avatar
0
Thank you for your help and feedback! The problem gives you an open curly bracket on the function which i hadnt noticed needs closing! 🙈 If my if statement isnt supposed to sit inside the function when would you close the function curly bracket?
23rd Mar 2021, 8:30 AM
Leanne Smith
Leanne Smith - avatar
0
Inside the curly braces is becomes a 'single scope of module' so Statements begin with { must be close with }. Otherwise error. You must add ; if you write 2 independent statements in single line to separate them. Javascript Ingore missing ; if statements are in different lines..(edit : i mean dont give you error). But better follow guidelines of adding semi colon..
23rd Mar 2021, 8:36 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 technically js doesn't ignore presence or missing of ; at end of line... javascript in non strict mode try to insert ; where it think it is missing... that could be a source of problems, and one of the reason for introducing "strict mode"... if you wrote: return a + b then js will interpret: return a; + b; wich doesn't throw explicit error, but give an unexpected return value ;)
23rd Mar 2021, 8:41 PM
visph
visph - avatar
0
visph yes. I mentioned thinking about playground here with non-strict mode. But i mentioned to follow rule of adding semi colon. Leanne Smith And forgot about strict mode. Yes . You must end statements with semocolon in strict mode.
23rd Mar 2021, 8:56 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I mentionned only "strict mode" to let understand that it is better practice to follow the ; rule at end of line ;) however, I have ear that you advice to put them :)
23rd Mar 2021, 9:00 PM
visph
visph - avatar