crea un switch donde se compruebe edad y altura in javascript

Here is an example code for a switch statement that checks both age and height in JavaScript:

index.tsx
let age = 25;
let height = 170;

switch (true) {
  case age < 18:
    console.log("You are under 18 years old.");
    break;
  case height < 150:
    console.log("You are under 150 cm tall.");
    break;
  case age >= 18 && height >= 150:
    console.log("You are eligible.");
    break;
  default:
    console.log("Invalid input.");
}
336 chars
17 lines

Explanation:

  • The switch statement is used to execute different code based on different conditions.
  • In this case, we want to check two conditions, age and height, so we use true as the condition to evaluate.
  • Each case statement checks for a specific condition and executes a corresponding block of code if the condition is true.
  • The break statement is used to control the flow of the switch statement; it stops the execution of the switch block.
  • The default statement is executed if none of the conditions match.

In the code above, we first define two variables, age and height. Then, we use the switch statement to check the values of age and height.

The switch statement checks each case statement in sequence until it finds a matching one. If none of the case statements match, it goes to the default statement.

In this example, if the age is less than 18, it prints "You are under 18 years old". If the height is less than 150, it prints "You are under 150 cm tall". If both age is greater than or equal to 18 and height is greater than or equal to 150, it prints "You are eligible". If none of these conditions match, the default statement is executed and it prints "Invalid input".

gistlibby LogSnag