Control flow / Control de flujo (if...else/switch...case)

PHOTO EMBED

Mon Oct 11 2021 12:26:57 GMT+0000 (Coordinated Universal Time)

Saved by @ianvalentino #javascript #controlflow #if...else #if #else #switch...case #switch #case

let userName = '';
/* Con nombre o sin nombre */
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
/* Se guarda la pregunta */
let userQuestion = 'Is this true?';
/* Se muestra la pregunta */
console.log(`Hey ${userName}! You just asked this: ${userQuestion}`);
/* Se genera un número aleatorio entre 0-8 */
randomNumber = Math.floor(Math.random() * 8);
/* Se guarda la respuesta en una variable */
let eightBall = '';
/* Se guardan distintas respuestas para el número que toque */
// takes in randomNumber, then eightBall = 'Reply'
// you can use if/else or switch;
// If the randomNumber is 0, then save an answer to the eightBall variable; if randomNumber is 1, then save the next answer, and so on.
/* Se utiliza switch */
/*
switch (randomNumber) {
  case 0:
    eightBall = 'It is certain';
    break;
  case 1:
    eightBall = 'It is decidedly so';
    break;
  case 3:
    eightBall = 'Reply hazy try again';
    break;
  case 4:
    eightBall = 'Cannot predict now';
    break;
  case 5:
    eightBall = 'My sources say no';
    break;
  case 6:
    eightBall = 'Outlook not so good';
    break;
  case 7:
    eightBall = 'Signs point to yes';
    break;
}
*/
/* Se utiliza if */
if (randomNumber === 0) {
  eightBall = 'It is certain';
} 
if (randomNumber === 1) {
  eightBall = 'It is decidedly so';
}
if (randomNumber === 2) {
  eightBall = 'Reply hazy try again';
}
if (randomNumber === 3) {
  eightBall = 'Cannot predict now';
}
if (randomNumber === 4) {
  eightBall = 'Do not count on it';
}
if (randomNumber === 5) {
  eightBall = 'My sources say no';
}
if (randomNumber === 6) {
  eightBall = 'Outlook not so good';
}
if (randomNumber === 7) {
  eightBall = 'Signs point to yes';
}
/* Se muestra */
console.log(eightBall);
content_copyCOPY

Magic Eight Ball You’ve learned a powerful tool in JavaScript: control flow! It’s so powerful, in fact, that it can be used to tell someone’s fortune. In this project we will build the Magic Eight Ball using control flow in JavaScript. The user will be able to input a question, then our program will output a random fortune.

https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1