Help with Magic Eightball Control Flows

I thought I was a good job following this until the very end. For some reason on step 8 my code doesn’t generate an eight ball response. The output always just reads "The eight ball says: ". There is always just a blank space at the end.

I tried rewriting everything and followed the instructional video and still can not fix the issue. Does anyone know what I might be doing wrong?

https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1?action=resume_content_item

My code:

let userName = 'James'

userName ? console.log(`Hello, ${userName}!`):
console.log('Hello!');

let userQuestion = 'Should I eat dinner?'

console.log(`${userName} asked ${userQuestion}`);

let randomNumber = Math.floor(Math.random() * 8);

let eightBall = ''

switch(randomNumber) {
  case 0:
    eightball = 'It is certain';
    break;
  case 1:
    eightball = 'It is decidedly so';
    break;
  case 2:
    eightball = 'Reply hazy try again';
    break;
  case 3:
    eightball = 'Cannot predict now';
    break;
  case 4:
    eightball = 'Do not count on it';
    break;
  case 5:
    eightball = 'My sources say no';
    break;
  case 6:
    eightball = 'Outlook not so good';
    break;
  case 7:
    eightball = 'Signs point to yes';
    break;
}

console.log(`The eight ball says: ${eightBall}`);

Look how you spelled eightBall in your switch block.

1 Like

I don’t mean to revive an old post, but combing through egcg’s code I notice that these two statements…

let userName = 'James'
let userQuestion = 'Should I eat dinner?'

…don’t end with a semicolon. Will this create issues in the code (after he fixes the spelling of eightball to eightBall) or is it not needed to declare the end of these statements? If it is not needed, what makes it so the computer needs it after say:

let randomNumber = Math.floor(Math.random() * 8);

I’ve been trying to understand code “anatomy” better which is why I’m asking.

In JavaScript ;'s are not required to end a statement like they are in other languages like C, C++, C#, Java, etc. The only reason you would need them is if you wanted to combine statements on a single line. For example:

let name = "dootmaster64"; console.log(`Hello, ${name}.`)

So, now the question is, “If they’re not required, why use them at all?” or “Why does Codecademy use them in the exercises?” I can’t speak for Codecademy, but my best guess is they are attempting to avoid confusion in some form or fashion. For me personally, I’ve stopped using them in JavaScript. I do now on occasion forget to put one at the end of a line of code in C++ or C#, but those omissions are easily spotted, and fixed when they occur. For you, if it helps to have the visual, “This is where the statement ends.” ;, then continue using them. If you’d rather not, don’t. The lack of ;'s will not keep your code from executing as long as each statement is on its own line. Accidentally putting a ; where it doesn’t belong, however, is a problem:

if(5 > 4); {
  console.log('5 is greater than 4.');
} else {
  console.log('5 is not greater than 4.');
}

Spot the mistake? It throws this error which can be misleading: SyntaxError: Unexpected token else

For a new coder, that error could be a bit confusing. What is wrong with my else?
Bottom line, if they help you, and you like them, use them. If not, don’t.

Happy Coding!

1 Like

(or if a line starts with [] or ())

"hello"
(3 + 1)

(string not callable)

sometimes dealt with like this:

"hello"
;(3 + 1)

look up the statement you’re writing. does it, or does it not, include a semicolon? write it accordingly (or don’t write it, which is simpler) (… or pick a better behaved language to learn since javascript is full of these things)

2 Likes

Thank you @midlindner and @ionatan for taking the time to address what i hope wasn’t too nit picky a question! I see i became hung up on the specific reasoning of it’s use but the distinction between the different language’s helps put things into perspective. I think just remembering which statement’s have a semicolon would be easier, but i see how the practice of using them can translate well into other languages.

As a total beginner to the coding scene I really appreciate the patience and time you’ve taken to help me today. Thanks again!

1 Like