Snippets Collections
function calculate(options) {
  const num1 = options.num1;
  const operator = options.operator;
  const num2 = options.num2;

  let sum;

  // if the operator does not equal to plus minus multiply or subtract
  switch (operator) {
    case "+":
      sum = num1 + num2;
      break;

    case "-":
      sum += num1 - num2;
      break;

    case "*":
      sum += num1 * num2;
      break;

    case "/":
      sum += num1 / num2;
      break;

    default:
      sum = "Sorry no operator assigned";
      break;
  }

  return sum; // dont forget to return sum after the switch has executed
}

console.log(
  calculate({
    num1: 3,
    operator: "+",
    num2: 4,
  })
);
let fruit = "Apple";
let message;

switch (fruit) {
  case "Banana":
    message = "Bananas are yellow.";
    break;
  case "Apple":
    message = "Apples are red or green.";
    break;
  case "Orange":
    message = "Oranges are orange, obviously!";
    break;
  default:
    message = "I don't know that fruit.";
}

console.log(message);
    let selectColor = '';
    let selectCategory = '';

    const getColor = (metadata) => {
        if (metadata !== undefined) {
            if (metadata.length > 1) {
                selectColor = 'orange';
            } else {
                switch (metadata[0]) {
                    case 'stageEarlyChildhood':
                        selectColor = 'pink';
                        break;
                    case 'stagePrimarySchool':
                        selectColor = 'green';
                        break;
                    case 'stageSecondarySchool':
                        selectColor = 'red';
                        break;
                    case 'stageLearnerNoviceDriver':
                        selectColor = 'blue';
                        break;
                    default:
                        selectColor = 'orange';
                        break;
                }
            }
        }
    };
getColor(object.somevalue.that.has.an.array);
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);
    switch (question.type) {
      case DRAG_AND_DROP_TEXT:
        answerOrder.push(question.dropspots.map((dropspots: any) => dropspots.id));
        break;
      case RANKING_TEXT:
        answerOrder.push(question.response.map((response: any) => response.id));
        break;
      case SINGLE_CHOICE:
      case MULTIPLE_CHOICE:
      case STATEMENT_CHOICE:
      case SINGLE_CHOICE_IMAGE:
        answerOrder.push(question.answers.map((answer: any) => answer.id));
        break;
      default:
        answerOrder.push([]);
    }
star

Thu May 02 2024 06:15:07 GMT+0000 (Coordinated Universal Time)

#switch #statement #function #object
star

Thu May 02 2024 05:56:18 GMT+0000 (Coordinated Universal Time)

#switch #statement
star

Thu Oct 06 2022 05:49:42 GMT+0000 (Coordinated Universal Time)

#switch #react #ifs #javascript
star

Mon Oct 11 2021 12:26:57 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1

#javascript #controlflow #if...else #if #else #switch...case #switch #case
star

Thu Mar 11 2021 14:34:21 GMT+0000 (Coordinated Universal Time) all-in-one-APP2\all-in-one\src\core\factories\courseDataProcessor.ts

#switch #case

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension