Creating functions in JavaScript – Part 1

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);
Enter fullscreen mode
Exit fullscreen mode
> "zero"
> "one"
> "two"
> "three"
> "four"
> "five"
> "The number is out of range"
> "The number is out of range"
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Mortal kombat legends : battle of the realms (2021). Net offer ssd storage ? get a host. Pilatus pc 12 initial training courses pilatus pc 12 recurrent training courses.