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