0

I am writing an array calculator on code wars. I have a function that checks the array for a valid sequence of numbers and operators. My function validSequence() is not return false when it is supposed to. The error in this test array:

Test.assertSimilar(validSequence(['10', '+', '20', '*','3', '*']), false)

is not being caught by this line in validSequence()

if(typeof parseInt(arr[arr.length-1]) !== 'number'){
    return false;
  }

and I don't know why. It gives a NaN as expected, so I don't know why it doesn't enter the block and return false.

user137717
  • 2,005
  • 4
  • 25
  • 48

1 Answers1

2

That's because NaN has the number type. Instead, use isNaN to detect if parseInt returns a rational number.

if(isNaN(parseInt(arr[arr.length-1]))){
    return false;
  }

parseInt will always return a rational number, or NaN, so there is no reason to check the variable type, as it will always be 'number'.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171