0

I can't seem to get this test to pass:

hasNull([1, null, 3])

Expected: true but got: false

My function:

function hasNull(arr) {
  return arr.some((item)=>{ item === null }) ? true : false
}
norbitrial
  • 14,716
  • 7
  • 32
  • 59
ju_ro
  • 37
  • 3
  • 4
    You're not returning from the arrow function. Change it to `arr.some((item)=> item === null )`. You don't need the ternary, since `Array.some()` returns a boolean anyway. – Ori Drori Jan 03 '20 at 19:12
  • 1
    The ternary is useless, it already returns a Boolean. – epascarello Jan 03 '20 at 19:12
  • When you specify {}, you must use `return`. Just remove {} and your solution should work. – w35l3y Jan 03 '20 at 19:17
  • 1
    You're doing `true ? true : false` or `false ? true : false`. The ternary is redundant. – user229044 Jan 03 '20 at 19:30
  • Relevant dupe: https://stackoverflow.com/questions/28889450/when-should-i-use-return-in-es6-arrow-functions – VLAZ Jan 03 '20 at 20:08

2 Answers2

4

I made it a bit shorter then your solution. If you remove the curly brackets {} from the array function then it will return the value of item === null check other than that it is just undefined that's why it does not pass any value for some() from your array.

Plus one suggestion is ternary operator in this case is useless because Array.prototype.some() returns a boolean already, see the documentation:

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

I guess this can work for you with the following one liner:

const hasNull = arr => arr.some(item => item === null);

console.log(hasNull([1, null, 3]));

I hope that helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • 3
    `const has = value => arr => arr.some(item => item === value); const hasNull = has(null);` if they want to create other kinds of checks as well :) – Patrick Roberts Jan 03 '20 at 19:24
0

You're missing return statement:

function hasNull(arr) {
  return arr.some((item) => { return item === null })
}
sasha
  • 21
  • 2