Magic Eight Ball [Exercise]

  • In this project I built a Magic Eight Ball game using switch and case statemtnts.

  • The user will be able to input a question and then output a random fortune.

let userName = '';

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

let userQuestion = 'What is this game?';

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

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

let eightBall = '';

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

  console.log(eightBall);
}

// Output: Hello!
//          , What is this game?
//          My sources say no

In the above example everytime we run the code it results in a different result.

Last updated