Hi, what´s wrong with this code? Everytime when i run the code the numbers of the code (1,2,3, etc.) are printed to the console. But the console.log('It is certrain') aren´t logged into the console. Whats wrong with it?

Hi, what´s wrong with this code? Everytime when i run the code the numbers of the code (1,2,3, etc.) are printed to the console. But the console.log(‘It is certrain’) aren´t logged into the console. Whats wrong with it?

https://www.codecademy.com/workspaces/61dda9d7e825144941828ac9

Thanks

Welcome to the forums!

The issue is that the random number being generated (and therefore the eightBall variable) contains an integer but the conditions in the cases are all strings, meaning none of the cases can ever run.

For example:

  case '0':
    console.log('It is certrain');
      break;

Will never execute because eightBall can never be a string with value "0", as it’s a number.

Can you give me one example with the right way to write it?

var userName = ‘…’

userName ? console.log(hello ${userName} !) : console.log(‘hello.’);

const userQuestion = ’ do you like video games’

console.log(the user asked ${userQuestion})

var randomNumber = Math.floor(Math.random() * 8)
let eightBall = ${randomNumber}

console.log(‘The eight ball answered:’)

if (randomNumber === 0 ) { console.log(’ it is certain’)}

if (randomNumber === 1 ) { console.log(’ Signs point to yes’)}

if (randomNumber === 2 ) { console.log( ‘Do not count on it’)}

if (randomNumber === 3 ) { console.log( ‘Cannot predict now’)}

if (randomNumber === 4) { console.log( ‘Reply hazy try again’)}

if (randomNumber === 5 ) { console.log( ‘It is decidedly so’)}

if (randomNumber === 6 ) { console.log( ‘Outlook not so good’)}

if (randomNumber === 7 ) { console.log( ‘My sources say no’)}

Since eightBall will be a number, you have to use numbers as the case statements, for example:

case 0:

instead of:

case "0":

This is because for a case statement to match, not only must it be the same value, but it must be the same datatype (so a number would not match a string, it must be two strings or two numbers).

While in general, it’s best not to copy whole blocks of code as this can inhibit understanding and it’s usually much better in terms of learning and understanding the error to try to work out what’s actually going wrong instead, you can see the differences in the code @roborou sent :slightly_smiling_face: