Why is my code wrong?


#1

let moonPhase = “full”;
if(moonPhase===“full”)
console.log(“Howwwwlll!!”);
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”);


#2

Hi @scriptrockstar71936,

Check over the if/else if/else syntax.

See if your code is following this format:

if (......) {
  //....your code
} else if (..........) {
  //....your code
} else if (...........) {
  //....your code
} else {
  //....your code
}

Hope this helps :slight_smile:


#3

Hi @codexthon my code is following that format.
I dont need to use ‘{ }’ becasue there is only one line after the if statement


#4

Hi @scriptrockstar71936,

I usually do not omit { } curly brackets even if it’s one line statement. I looked over:

If that’s the case, then your code should work.

If only you can provide more details of your situation, what error it throws, a link to the exercise, it could be easier for us to decipher.

If you’re saying your code wrong because of the lesson wouldn’t let you pass, then you might want to try the suggestion I posted above. Sometimes, the checking system behind just want exactly that, OR even if there’s a lack of a space, difference in spelling or something else.

Cheers :slight_smile:


#5

The following is valid JS:

const randint = n => Math.floor(Math.random() * n + 1) ;

let a = randint(10);
let b = randint(10);
let c;

if (a < b) c = -1;
else if (a > b) c = 1;
else c = 0;

Style guides generally recommend against this type of writing, though. Granted, a single line can be seen as not being a code block, but the fact remains, it is one. Some would even maintain that as demarcated blocks, the code is more readable.

Having a style rule that allows both simple inline and complex blocks means risking inconsistency and does not bode well when forming good habits.

if (a < b) { c = -1; }
else if (a > b) { c = 1; }
else { c = 0; }

is also valid, but the blocks are kind of hidden, still. Ideally, JavaScript that is meant to be readable should take full advantage of whitespace and structure.

if (a < b) { 
    c = -1; 
}
else if (a > b) { 
    c = 1; 
}
else { 
    c = 0; 
}

Now in the case above, the assignment is always to the same variable, c. There is an alternative to if, the ternary expression statement.

c = a < b ? -1 : a > b ? 1 : 0;

Here we need no parens or braces. The ternary operator, ? treats the expression before it as a boolean and the true branch is the first term immediately following, else the one on the right side of the colon. Notice we inserted a conditional expression in the false branch. This emulates, else if. The final term is the default (else).

As these lessons go, it can be assumed that expected code will follow common patterns and writing structure (white space and indents). We should never take liberties when first attempting lessons. After you pass, go to town, but leave it to the end of the unit module since many lessons pass the code to the next lesson.


#6

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