The aim of this puzzle: Use an if-else statement to simplify the Flip a Coin puzzle.
Walkthrough of the solution: This puzzle starts out very similar to the original Flip a Coin puzzle, but instead of 2 if statements, there’s an if-else statement.
An if-else statement is really just a regular if statement with another set of curly brackets. That second block {} of code will only run if the code in the if statement parentheses is false. So, the if statement checks the parentheses and if it’s true the first code block runs, and if it’s false the second code block runs. Only one will run, not both or neither.
In this puzzle, there are only two categories that the answer can be: heads or tails. If it’s not ‘heads’ then it must be ‘tails’. That means we can just check for ‘heads’ in the first if statement and if it doesn’t pass the test, the else block will run and we know the answer is ‘tails’. So in the else block, we just need to add a drawBoxes() call with a string.
Would you be able to share a screenshot of your code so we can investigate further? Or share it with us via the ‘Report an issue’ option, available from the three dot menu on the top right corner?
Seems like I’m doing it right as well, just can’t see where I’m going wrong. It draws the tails image correctly, and it’s randomized, just not letting me pass.
When you put an If Statement inside the brackets {} of another If Statement, that means the inner If Statement will only be checked if the outer one is true. In your code, think about what happens if answer === 'tails. The 1st if (answer === 'head') will be false, so the code inside its block {} will not run – that means the other If Statement won’t run. Try using 1 If-Else Statement for this puzzle. That way, if the if() part is false then the else {} part will run.
Test Condition
The parentheses () of an if() should have code that is either true or false. Usually you want to use ===, >, or <. You have drawBoxes('rrr wrw wrw') inside the parentheses. That code should instead go between the curly brackets {} of an If or Else Statement.
Your code is printing out heads, tails instead of a random choice of one or the other. That’s because answer is storing the entire array. When you use pickRandom(answer) on its own line, it creates a string (either ‘heads’ or ‘tails’) but then that string does nothing.
Instead of storing an array in answer try storing pickRandom(['heads', 'tails']). That way, the answer will store either ‘heads’ or ‘tails’.
Hi i have a problem I don’t know what I am doing wrong at this level and I don’t know where to ask help so I Justin do it here here are some screenshots.wel I just found out new users only can’t send one screenshot