JavaScript Control Flow Conditionals

Else

An if statement followed by zero or more else-if statements can optionally be followed by an else statement. The code block for else is executed if and only if none of the other conditions are truthy.

if (condition) {
// execute the code within this code block
} else if (condition2) {
// code block for condition2
} else {
// code block for else
}

Test yourself

Description

Define a new function with the name

numToWord

that takes an integer input parameter x and returns the number converted to its English written word equivalent. The function is only supposed to convert numbers up to 10, and return all other numbers as strings.

{
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
10: "ten",
}

Try out different strategies. The easiest might be a whole bunch of if, else if and else statements, but maybe you can figure out a more elegant solution?

Tests

  1. 1 You should define a function named numToWord.
  2. 2numToWord(0) should return "zero".
  3. 3numToWord(1) should return "one".
  4. 4numToWord(2) should return "two".
  5. 5numToWord(3) should return "three".
  6. 6numToWord(4) should return "four".
  7. 7numToWord(5) should return "five".
  8. 8numToWord(6) should return "six".
  9. 9numToWord(7) should return "seven".
  10. 10numToWord(8) should return "eight".
  11. 11numToWord(9) should return "nine".
  12. 12numToWord(10) should return "ten".
  13. 13numToWord(-1) should return "-1".
  14. 14numToWord(11) should return "11".
  15. 15numToWord(123) should return "123".
Editor