0
0 to "zero"?
Declare a function getSimpleNumberName. /** * @param {number} ??? - the number whose name should be returned (0-10) * @returns {'zero'|'one'|'two'|'three'|'four'|'five'|'six'|'seven'|'eight'|'nine'|'ten'} the name of the given number */ actual = getSimpleNumberName(0); expected = "zero"; Is there a way to convert the number 0 to the string "zero" quickly? Any help appreciated.
3 Respuestas
+ 3
You can split a string "Zero,One,Two,Three,Four,..." using comma ',' then obtain an element from the resulting array using <n> as index.
+ 2
I would suggest manually filling an array if you're bound to the range [0, 10]. Like this:
var nums = ["zero", "one", "two", ..., "nine", "ten"];
The answer for input n would then be nums[n].
Since arrays aren't actual arrays but rather objects that pretend to be arrays, accessing an index out of range would give you undefined in return, as would accessing a field that doesn't exist. But that shouldn't be a problem if you want your function to only behave correctly for a valid input.