المعرفة:: JavaScript الحالة::مؤرشفة المراجع:: JavaScript Essential Training


Conditional switch statement

The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

const usedStatus = () => {
  let age = everydayPack.backpackAge();
  let description;
 
  if (age >= 30) {
    if (age >= 365) {
      if (age >= 1095) {
        description = "old";
      } else {
        description = "used";
      }
    } else {
      description = "lightly used";
    }
  } else {
    description = "new";
  }
};
const usedStatus = () => {
  let age = everydayPack.backpackAge();
  let description;
 
  switch (true) {
    case age < 30:
      description = "new"
      break;
    case age >= 30 && age < 365:
      description = "lightly used"
      break;
    case age >= 365 && age < 1095:
      description ="used"
      break;
    case age >= 1095:
      description = "old"
      break;
    default:
      console.log(`There is no description for ${age}.`)
  }
};