Magic Eight Ball Question

I’m getting this message when I run the code:
/home/ccuser/workspace/learn-javascript-U2P1/main.js:21
});
^
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Here is the code. I’m missing something and can’t figure it out. Please help.

let userName = ‘James’;

userName === ‘’ ? console.log(‘Hello!’) : console.log(‘Hello,’ + userName + ‘!’);

const userQuestion = ‘Will this end?’;

console.log(userQuestion);

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

console.log(randomNumber);

let eightBall = ‘’;

switch (randomNumber); {

case 0:

eightBall = 'It is certain';

break;

case 1:

eightBall = 'It is certain';

break;

case 2:

eightBall = ‘It is decidedly so’;

break;

case 3:

eightBall = 'Reply hazy try again';

break;

case 4:

eightBall = 'Cannot predict now';

break;

case 5:

eightBall = 'Do not count on it';

break;

case 6:

eightBall = 'My sources say no';

break;

case 7:

eightBall = 'Outlook not so good';

break;

case 7:

eightBall = 'Signs point to yes';

break;

}

Hello @net8570744740, welcome to the forums! When posting code, please make sure to format it, by pressing this button:

This will then appear:
Screen Shot 2020-05-02 at 14.06.13
You can then paste/write your code in there.

Anyway, back to your question. From what I can tell by running the code you have provided, the issue is this:

Specifically, your semicolon. Because a switch statement leads onto case statements, you shouldn’t use a semicolon. Your switch line should therefore look like this:

switch (randomNumber) {

If you Google when to use and when not to use semicolons, you should find a range of websites, although with a cursory glance, I found this one.

Also, this line

appears twice (“outlook not so good” and “signs point to yes”) That means the second instance (“signs point to yes”) will never be printed.

I hope this helps!

let userName = 'James';

userName === '' ? console.log('Hello!') : console.log('Hello,' + userName + '!');

const userQuestion = 'Will this end?';

console.log(userQuestion);

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

console.log(randomNumber);

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

console.log(eightBall);

Thanks for that. I corrected it and ran the code. When I hit Save, nothing comes up and the save button just spins. Thoughts?

Try reloading the page, and redoing the exercise. Also, you could copy the code, close the tab, and then on re-opening, reset the code, then paste it back in. It seem long-winded, but its worked for me.

Also, you might want to look at this line

You are having two case statements which run if 7, that means the latter case will never run. You could change that to case 8:, which would mean you then have to change this:

To accomodate 8 possible choices (remember Math.random() gives an exclusive result-any number up to but not equal to the number you specified, in your case 8.):

const randomNumber = Math.floor(Math.random() * 9)

Awesome. Thank for your help. It works now!

1 Like