Please help what is wrong with my Magic 8 Ball code (javascript) I'm doing the project but it won't run

let userName=‘Jason’

userName ? console.log (‘Hello, $userName’)

: console.log (“Hello!”);

let userQuestion =‘Will I pass coding?’;

console.log(‘The user asked: ${userQuestion}’);

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

let eightBall= ‘’

switch(randomNumber)

{

Case 0:

eightball= ‘It is certain’;

break;

Case 1: ‘It is decidedly so’

break;

Case 2: ‘Reply hazy try again’

break;

Case 3: ‘Cannot predict now’

break;

Case 4: ‘Do not count on it’

break;

Case 5: ‘My sources say no’

break;

Case 6: ‘Outlook not so good’

break;

Case 7: ‘Signs point to yes’

break;

}

console.log(eightBall);

Sorry my question is what is wrong with this code, it won’t run.

You need to put the code inside a function, and then call the function. Example

const eigthBall = () => {
//Your code
};

//Call your function
eigthBall();

I just completed this assignment and I don’t think functions are needed (that’s in the next unit).

Initial issues I’m seeing with your code @civilrights_83 are:

  • userName ? console.log (‘Hello, $userName’)userName ? console.log(Hello, ${userName})
  • Missing variable eightball in your switch() cases (which means it doesn’t know what to do with the strings you’re inputting.
  • Missing semicolon ; after cases 1-7

There may be some other errors, but that’s what I see so far. I added some other elements to mine so it’s hard to compare them.

In Case 0, you assigned the string ‘It is certain’ to the variable eightball. I’m not sure what you expect from the other cases, but you’re not assigning those string values to eightball, or anything for that matter.

Case 1:
eightball = ‘It is decidedlyso’;
break;
Case 2:
eightball = ‘Reply hazy, try again’;
break;

etc…

Couple of problems that we need to fix before we can begin to debug this.

First off, your single quote characters don’t seem to be recognised by my text editor when I copied your code. I had to change all these. Are you using a special keyboard or using some other character (make sure it’s either an ' or an " and not an ` or whatever that character is).

Second issue, the word case should be lowercase and not capitalized.

Third issue. In case 0, you assign a string to eightball (should this be eightBall ? Keep variable names consistent!), but in all of the following statements, there are no assignments. Check your code again for consistency.

And last comment I would make is a suggestion. I would put all of this inside a function, and then you can decide whether your function should console.log something, or return something. If returning a value, then the break statements will become redundant and should be removed.

If, on the other hand, you are reassigning the variable instead of directly returning it within each case statement, then I would expect you will need a return statement before exiting the function, i.e. before the final }, but I haven’t fully explored this.

Finally, I would place the randomNumber assignment inside your function body, so that it is executed afresh every time there is a call to this function.

I had a play around with this in my browser, and came up with something like this (spoiler alert):

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

console.log(pickAnswer());

I hope these suggestions help somewhat. Keep coding! :slightly_smiling_face:

P.S. I forgot to say welcome!