Ifā€¦else else if Statements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Ifā€¦else else if Statements

Please help me with what is wrong with my ifā€¦else/ else if code. Thank you. https://code.sololearn.com/WP9NoSKG511l/?ref=app

16th Feb 2022, 5:21 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
12 Answers
+ 3
TahitišŸ·Castillo As I understood till now your code should be like this just in few lines: One more question why did you use parseInt when you are entering string value? https://code.sololearn.com/WBmU7LCALQ0S/?ref=app
16th Feb 2022, 9:26 AM
AĶ¢J
AĶ¢J - avatar
+ 4
If there are multiple statements in an if or else block, enclose them inside a pair of {}. You didn't do that, that's why you're getting this error.
16th Feb 2022, 5:23 AM
Rishi
Rishi - avatar
+ 4
TahitišŸ·Castillo For multiline statement we should use {} And also you are checking variable before assigning element value which is also wrong there should be: rat = document.getElementById('rat'); if (rat != null) document.write(p); Also no need to write same thing inside every condition. document.write(p); is same inside every condition. You can write this after final else block.
16th Feb 2022, 5:28 AM
AĶ¢J
AĶ¢J - avatar
+ 4
TahitišŸ·Castillo I did something that might help you. Input the word "cancer" and it'll tell how it looks like. Maybe not what you wanted but you'll get an idea by looking at it https://code.sololearn.com/WHfV08chfkEY/?ref=app
16th Feb 2022, 7:39 AM
Rishi
Rishi - avatar
+ 4
AĶ¢J Oh, my gosh! You fixed it! I can hardly wait to get off work and look over your code. What I did was read various website explanations of how to achieve the objective, and there was one that gave the parseInt code, which makes no sense now that Iā€™m thinking about it, because there are no integers involved. Not sure why I overlooked that. Thank you. šŸ™
16th Feb 2022, 4:43 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 3
Rishi Only one pair of {} for the whole thing? Or individually? Thank you!
16th Feb 2022, 5:24 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 3
AĶ¢J If I donā€™t give the instruction inside each condition, then what goes inside each { } ?
16th Feb 2022, 5:53 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 3
TahitišŸ·Castillo what do you want to do actually? What are you trying to do with the if else if block?
16th Feb 2022, 6:50 AM
Rishi
Rishi - avatar
+ 3
AĶ¢J Iā€™m just wondering why I agonized for hours, unnecessarily typing dozens of lines of JS code, only to find out it only required <25 lines. šŸ˜© I guess this is part of the initiation into the tech world. šŸ˜ Thanks again.
17th Feb 2022, 3:19 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 3
TahitišŸ·Castillo It is natural in starting learning. That's why practice is more important. As much as you will do practice your thinking level would be increase.
17th Feb 2022, 3:29 AM
AĶ¢J
AĶ¢J - avatar
+ 2
Rishi A prompt should ask what is your zodiac animal. Entering the animal name triggers a description written to the doc/screen.
16th Feb 2022, 7:00 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 2
Rishi Thank you. That makes sense. But the info I want to output is lengthy. I have whole paragraphs hidden in HTML that I want to display based on the input.
16th Feb 2022, 4:36 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar