5. else if statements


#1

Hi, so I was pulling my hair out trying to figure out why my code wouldn’t push me to the next step, even though when tested it would return the correct answer.

var moonPhase = ‘mostly full’;

if (moonPhase === ‘full’)
{
console.log(‘Howwwlll!!’);
}
else if (moonPhase === ‘mostly full’)
{
console.log(‘Arms and legs are getting hairier’)
}
else if (moonPhase === ‘mostly new’)
{
console.log(‘Back on two feet’)
}
else
{
console.log(‘Invalid moon phase’);
}

Now having found a someone who had an issue with the next step, i copied their code to see if it worked, or if it was a bug, and low and behold it worked just fine.

var moonPhase = ‘solar eclipse’;

if (moonPhase === ‘full’) {
console.log(‘Howwwlll!!’);
} else if (moonPhase === ‘mostly full’) {
console.log(‘Arms and legs are getting hairier.’);
} else if (moonPhase === ‘mostly new’) {
console.log(‘Back on two feet’);
} else {
console.log(‘Invalid moon phase’);
}

Using a side by side comparison, the only difference between their code and mine, was the use of semi colons at the end of all the console.log statements. On a whim, i decided to check to see if my formatting was causing the error, which it was.

Could someone explain to me where my formatting is breaking things? Or is this a case of the codecademy lesson expecting a very specific format?


#2

because the validation is done using regular expressions (parts of it), so if and { have to be on the same line for this specific regex to work


#3

@britishwaldo no need to make a duplicate topic


#4

@stetim94 I was just about to delete this topic, as i realised i’d created it in the wrong section. It should have been in the learn javascript :control flows section. i had tried to move it, but i couldnt find the correct sub forum within the drop down.


#5

And can i ask you to clarify what you mean by validation? Are you referring to codecademy validation to make sure you’re doing it right, or are you talking about some javascript validation?

If you are referring to codecademy validation, then why is this the first time the issue has cropped up, not only that, but why in the subsequent instructions (mostly full, mostly new, solar eclipse) does it not matter if i then go back and change the formatting to put the opening { for else on a new line? I.E Why does it only break for the first instruction and none of the subsequent ones?

One reason I ask is so that I can try to understand when in future lessons I need to follow, what I consider very messy, code formatting, and when I can stick to what, from my view, is cleaner and easier to digest.

Another reason i ask is that I’ve been using that formatting for everything i’ve done so far, and havent had a single issue, not only this but it only has an issue with the else, nothing else. i could put the if { on 2 separate lines, 5 lines apart and it doesn’t have an issue. but do anything but else { and it breaks.

added for clarity:

var moonPhase = 'full';

if (moonPhase === 'full') 
	{
  	console.log('Howwwlll!!');
	} 
	else if (moonPhase === 'mostly full')
    {
      console.log('Arms and legs are getting hairier');
    }
	else if (moonPhase === 'mostly new')
    {
      console.log('Back on two feet');
    }
else {
    console.log('Invalid moon phase');
  }

this is fine.

var moonPhase = 'full';

if (moonPhase === 'full') 



	{
  	console.log('Howwwlll!!');
	} 
	else if (moonPhase === 'mostly full')



    {
      console.log('Arms and legs are getting hairier');


    }
	else if (moonPhase === 'mostly new')



    {
      console.log('Back on two feet');
    }
else   {
    console.log('Invalid moon phase');
  }

this is also fine.

var moonPhase = 'solar eclipse';

if (moonPhase === 'full') {
  console.log('Howwwlll!!');
} else if (moonPhase === 'mostly full') {
  console.log('Arms and legs are getting hairier.');
} else if (moonPhase === 'mostly new') {
  console.log('Back on two feet');
} else
 {
  console.log('Invalid moon phase');
}

this is broken.

cheers


#6

yes, the codecademy exercise validation. Javascript doesn’t have validation in that sense, sure if you have a mistake you might get an interpreter error message

that depends on how the lesson is validate. Some lessons might have it while others do not


#7

So what you’re saying is that in the future, if my code is correct and it runs correctly, to go back and format it exactly as the example/hint shows? (since there may be a different form of validation)


#8

that would be good advise, but i think this problem is in very few lessons


#9

okay thank you very much stetim94, I was pulling my hair out trying to figure out why it didnt like my code.

Apologies for the duplicate threads, like i said i was going to delete this one once i’d created the new one in the correct section.


#10

Good luck with that, you can’t delete topics. I already moved it to right category


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.