I was working with Hacker Rank tool.
Over there, all the input given by user is a string
So the input given by the user will be
1,2,3,4,5 which could be taken as "1"."2","3","4"
Now, I want to convert it into array of numbers
if (input) {
if (input.split(',').indexOf("") > - 1) { console.log("invalid input") }
else { input = input.split(',').map(Number); }
} else {
input = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8];
}
Here, I am checking if there is an input specified, if yes it will take that input defined by user or else it will take the input defined by me as inpurt
Now, the input given by the user can be string as well in which case I want to throw an error saying "invalid input"
For which I thought of something like this
if (input.split(',').indexOf("") > - 1) { console.log("invalid input")
But that doesn't appear to be working, can someone help me in figuring out how can I do it?
Update: When I say input given by user could be string, I mean he can give something like "1, 2, 3, abc" which is invalid input (since it contains abc)
Example: 1, 2, 3, 4 when consoled log gives "1, 2, 3, 4" which is a string basically having numbers ( a valid input)
but
Input of this
1, 2, 3, 4abc is invalid input because it consist of 4abc which isn't number
So when I do
input = input.split(',').map(Number);
for the above, it will probably give me
[1,2,3,NaN]
inpurtininpurt = input.split(',').map(Number);is a typo, right?inputvalue ininpurtNo Specific reason though, Can do something like this as wellif (!input) input = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8]4abcis a hexadecimal number.