0

I highlighted the parameter that I don't understand where its value comes from. Shouldn't the array get passed? it makes no sense. It all feels backwards. How does the array values get into the ages.some(checkAdult) invocation? Does the keyword before the dot become the argument that I imagine should be in parenthesis after checkAdult? Why doesn't it go some(checkAdult(ages))? Why append it to the end of something with a dot?

var ages = [3, 10, 18, 20];

function checkAdult(age) { // THIS LINE HERE, how does it link to the array???
  return age >= 18;
}

function myFunction() {
  document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
<p>Click the button to check if any of the elements in the array has a value of 18 or more.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • 2
    The `.some()` method passes each array element to the callback function. – Pointy Aug 15 '20 at 20:59
  • am i missing something about functional programming? why do this? why isnt there just a method that searches an array directly without invoking a seemingly uneccessary function call? – Lloyd Short Aug 15 '20 at 21:01
  • whats even the point of the checkAdult() function existing? – Lloyd Short Aug 15 '20 at 21:02
  • Have you had a look at the doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some ? The point of the function existing is that, when the "some" function is invoked on the array, it returns true if for at least one of the values in the array, the function returns true – Chris Neve Aug 15 '20 at 21:06
  • 1
    @LloydShort Yes, you might need to learn some more functional programming. I don't see what you mean by "*a seemingly unnecessary function call*". There are methods to search an array for a specific value, such as `.indexOf` or `.includes`, but for evaluating **arbitrary conditions** such as `>= 18` one needs a generic `some` function taking a callback. – Bergi Aug 15 '20 at 21:09
  • @LloydShort "*whats even the point of the checkAdult() function existing?*" - we can't tell, it's your code! Or if you have read it somewhere else, please cite the source, both for proper attribution and to give us some context for answering your question. – Bergi Aug 15 '20 at 21:10
  • (I suspect it's an example from a book, see also [all these other questions about `checkAdult`](https://stackoverflow.com/search?q=checkAdult)) – Bergi Aug 15 '20 at 21:23
  • Thanks everyone. I understand the syntax now. & its from an online article which is likely taken from a book. – Lloyd Short Aug 15 '20 at 23:04

1 Answers1

0

Typically (but not always) the user-supplied function is called by the higher-order function.

In the example below, some, calls f with each element of t, until the first truthy result is found. Otherwise a false result is returned.

const some = (f, t) =>
  t.length === 0
    ? false
    : Boolean(f(t[0])) // f gets called by some
      || some(f, t.slice(1))

console.log(some(v => v > 30, [ 10, 20, 30, 40 ]))
// true

console.log(some(v => v > 99, [ 10, 20, 30, 40 ]))
// false

Or an iterative version of the same program -

function some (f, t)
{ for (const v of t)
    if (Boolean(f(v)))
      return true
  return false
}

console.log(some(v => v > 30, [ 10, 20, 30, 40 ]))
// true

console.log(some(v => v > 99, [ 10, 20, 30, 40 ]))
// false
Mulan
  • 129,518
  • 31
  • 228
  • 259