Javascript - creating "some" function

An attempt to create "some" function. that is, return true if any of the elements in the array satisfies the condition

function some(array, predicate) {
  for (var i = 0; i < array.length; i++) {
    if (predicate(array[i]))
      return true;
  }
  return false;
}

console.log(some([NaN, 3, 4], isNaN));
// → true
console.log(some([2, 3, 4], isNaN));
// → false

      

Question 1: The above is the solution in the book and I'm not sure why "return false" would overwrite "return true" in every case. The above solution seems to assume that "return false" will only be executed if "return true" was never fired - WHY?

My solution looked like this.

var some = function(array, condition) {
  var answer = false;
  for(var i=0; i<array.length; i++) {
    if(condition(array[i])) {answer = true}; 
  }
  return answer
};

      

Question 2: Is the book's solution better because it takes up less memory (ie the "answer" variable)?

Thank!

+3


source to share


5 answers


return

controls where the progress bar is for the currently executing script. When encountered return

, it moves the execution pointer out of the function, and as a result, the function stops executing. This means that if it return true

does, return false

it won't.



+2


source


The book's solution is based on the fact that a keyword return

used in the body of the loop for

will cause the loop to stop and return.

Take a look at the section Interrupt function in return which says:

The return statement ends the execution of the function and sets the value to be returned to the caller of the function.

In your example, once it is matched condition

, the book's solution will no longer continue to iterate over the array and return (since that's what is returned in the definition above).

Have a look at your solution:



var some = function(array, condition) {
  var answer = false;
  for(var i=0; i<array.length; i++) {
    if(condition(array[i])) {answer = true}; 
  }
  return answer
};

      

What happens if the condition is met? You set the value answer

to true, but the loop will continue to check for subsequent elements in the array.

Consider a use case where the array is very large and you want to find a specific value in the array.

Which one do you think would be more efficient: one that stops after it finds a match, or one that returns a match only after looking at all the elements?

In this case, the answer will be the last one. Don't keep scrolling through all the items if you've already found what you want.

+2


source


When execution hits a statement return

, it leaves the current function no matter what block it was in within that function.

As soon as an element of the array matches the predicate, it returns true. If execution exits the loop for

normally, it means that none of the values ​​were valid, so we can return false.

The book's answer is better, not because it doesn't create an extra variable (it doesn't matter), but because it doesn't check all the variables every time. Once the array element is valid, it will exit the function.

+1


source


You can use the built-in method Array

, some()

:

var test = [NaN, 3, 4].some(isNaN); // true

      

+1


source


What this function does is

function some(array, predicate) {           // return a boolean value (true, false)
                                            // as soon an Array key matches `predicate`

  for (var i = 0; i < array.length; i++) {
    if (predicate(array[i])) return true;   // if Array key isNaN exit function as `true`
  }
  // We're 1. already out of the function or 2. all keys are looped.
  // This function needs to return a boolean so exit with `false`
  // since all Array keys are Numbers
  return false;                             

}

      

the above explains it all. Remember the part that says:
" returns a boolean value (true, false) as soon as the Array key matchespredicate

"
In your example, you do not exit the function, so if, for example, you passed an array of 1000 keys to a file , each individual key would be to loop, which the function shouldn't do as it's intended for it:

Return (exit) as true

if at least one key matches the predicate condition

Completing all other keys is useless as we have already found that one key matches.

Yes, it var

consumes interpreter memory (which is nothing to worry about).

0


source







All Articles