Create a function that takes a number from 0 to 5 and returns the English word for that number. For example, 1 should return “one”.
const numbers = [ ["1", "one"], ["2", "two"], ["3", "three"], ["4", "four"], ["5", "five"]
] const stringFromNumber = (num) => { if (num === 0) { const value = "zero" console.log(value) return value; } else if (num >= 1 && num <= 5) { const getIndex = numbers[num - 1]; console.log(getIndex[1]) } else { console.log("The number is out of range"); }
} stringFromNumber(0);
stringFromNumber(1);
stringFromNumber(2);
stringFromNumber(3);
stringFromNumber(4);
stringFromNumber(5);
stringFromNumber(-1);
stringFromNumber(6);
> "zero"
> "one"
> "two"
> "three"
> "four"
> "five"
> "The number is out of range"
> "The number is out of range"
Discover more from Coursity
Subscribe to get the latest posts sent to your email.