Fundamentals3 / 5
Up next

Control flow

type Grade = "A" | "B" | "C";

// if/else plus a ternary picks a grade
function grade(score: number): Grade {
  if (score >= 90) return "A";
  return score >= 80 ? "B" : "C";
}

const scores: number[] = [95, 82, 70];
for (const s of scores) {
  switch (grade(s)) {
    case "A":
      console.log(`${s}: top`);
      break;
    default:
      console.log(`${s}: ${grade(s)}`);
  }
}
What you're solving

if/switch, for-of, and the ternary operator