The Magic eight Ball

can someone check this for me it works but i feel like i missed something with the eightBall variable, it should look different but i dont know how, thank you everyone for your help much appreciated!!

let userName = ‘Andrew’
userName ? console.log(Hello ${userName}!)
: console.log (‘Hello!’)

const userQuestion = Where is the butter ${userName}?
console.log(userQuestion)

let randomNumber = Math.floor(Math.random() * 8)
console.log(randomNumber)

let eightBall = ‘’
console.log()

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;
default:
console.log(‘Signs point to yes’);
break;
}

To preserve code formatting in forum posts, see: How do I format code in my posts?

In your current code, eightBall doesn’t serve any purpose. In your switch cases, you are directly printing out the strings.

Instead, in your cases, you could assign the strings to the eightBall variable and then have a single console.log statement outside and below the switch statement i.e.

// eightBall declared and initialized as empty string
let eightBall = ""

// New value assigned to eightBall within the matching case
switch (randomNunber) {
    case 0:
        eightBall = "It is certain";
        break;
    case 1:
        eightBall = "It is decidedly so";
        break;
    ...
}

// The value assigned to eightBall can be printed after the switch statement
console.log(eightBall);

Thank you very much for your help!!

1 Like