Reputation: 11830
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]
Upvotes: 2
Views: 127
Reputation: 11830
oKay, So I am using since I didn't want to method which iterates over an array rather wanted a single iteration to do the Job.
The above answers are amazing but for some reason I thought sharing my answer as well.
if (input) {
input = input.map(number => {
if (!Number(number)) console.log("Error: Contains a non number")
if (Number(number)) return Number(number)
}) else {
input = [7, 8, 4, 9, 9, 15, 3, 1, 10];
}
Upvotes: 0
Reputation: 36319
Ok, now I see what you need, thanks.
It's a pretty simple check to filter out bad numbers:
const numbers = input.split(',').map(Number)
const filtered = numbers.filter(n => !isNaN(n))
So in this case, numbers which can't be parsed by Number() will return NaN
. NaN
is falsy, so will be filtered out, and so the filtered
array has only numbers. To be clear !!n
just means to evaluate n as a strict boolean; so falsy becomes a real false. It works the same if you just used n
but I think it's more explicit to cast it as a bool
If you wan to throw an error, you can test:
if (numbers.length > filtered.length) throw new Error('Invalid Input');
Upvotes: 1
Reputation: 430
You can do something like below
var number = ['1', '2', 'a', 'b', '4', '5', '7']
function check() {
var s = 0;
for (var i=0; i < arguments.length; i++) {
if(!isNaN(arguments[i])){
number[s] = arguments[i];
console.log(number[s++]);
}
}
}
check(...number);
Upvotes: 1
Reputation: 5920
Paul's answer is almost correct. In the filter function, though, you need to also parse the number and then check if it's not a NaN
.
const numbers = input.split(',')
.filter(n => !isNaN(parseInt(n, 10)))
Upvotes: 1