2

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]
6
  • 1
    inpurt in inpurt = input.split(',').map(Number); is a typo, right? Commented Dec 19, 2018 at 7:21
  • nope. I am storing the input value in inpurt No Specific reason though, Can do something like this as well if (!input) input = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8] Commented Dec 19, 2018 at 7:22
  • I don't understand what you mean "the input given by the user can be string as well ". Earlier you said all input would be a string? Commented Dec 19, 2018 at 7:24
  • you can use inpurt.push(input) Commented Dec 19, 2018 at 7:25
  • 1
    4abc is a hexadecimal number. Commented Dec 19, 2018 at 7:36

4 Answers 4

1

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)))
Sign up to request clarification or add additional context in comments.

5 Comments

why that parseInt? totally needless.
Sorry. Old habit; used to doing it in one go without using the map.
Still, parseInt is less reliable than using the Number construct. Do that with eg '1,001,2,3' as input. The 001 is not valid in OP's context., but is in yours code.
what does n, 10 in parse int means?
n is the string you are trying to parse while 10 is the radix and a optional number (from 2 to 36) that represents the numeral system to be used. According to w3schools.com/jsref/jsref_parseint.asp
1

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);

Comments

1

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');

3 Comments

This will disallow 0 even though it's a perfectly valid number.
to exclude NaN, you could use .filter(n -> !Number.isNaN(n)) instead because yours approach excludes 0.
Good catch, thanks @KarelG, I was obviously trying to be too clever
0

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];
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.