Intermediate Algorithm Scripting: Everything Be True

Intermediate Algorithm Scripting: Everything Be True
0

#1

Hi Everyone,
Can someone please explain, why with the .every method, we have to explicitly include a return ?

The MDN documentation states that if the callback satisfies all the elements, true is returned. If it encounters an element that does not satisfy the condition, false is returned and the execution is stopped.

The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value. If such an element is found, the every method immediately returns false. Otherwise, if callback returns a truthy value for all elements, every returns true.
If this is correct, why do we have to specify a return explicitly ?

Also, the code only works if we include a return before the function declaration. Assigning the result to a variable and returning the variable does not work.
The code below returns undefined in the Chrome console

function truthCheck(collection, pre) {
  // Is everyone being true?
  collection.every( coll => { let val = (Boolean(coll[pre]) )
		return val;
	})
}

I struggled with this for quite some time and would like to understand why

Thanks !


#2

You have to give a return because that is how the .every function knows what it’s testing for.

Also, I would not write an every statement the way you did. I think your use of arrow functions is awkward. I would instead write it like:

function truthCheck(collection, pre){
  return collection.every(element => element == true);  //Not sure what your condition is, but I'm assuming just to see if something is truthy
}

#3

Thanks for your feedback on the arrow function. Definitely helps.

Nowhere on the web am I able to find the need for this return. Guess it is something to remember for the next time.


#4

MDN does show examples of the return though?


#5

.every() applies a given function to each element in an array and returns false as soon as it sees a false value returned from a callback.

Without explicitly returning true/false from the callback how would you determine the result of an operation?

In this way, having to explicitly return is a common sense that you can derive or observe.


#6

So, let me elaborate. I understand the need for a return in the callback, but the code does not work unless there is a return preceding the actual .every method invocation statement.

What actually works is

function truthCheck(collection, pre) {
  // Is everyone being true?
  return collection.every( coll => { return (Boolean(coll[pre]) );
	})
}

I tried out a few different options and none of them worked.

This was one of my options. The code below returns undefined. There is a return in the callback, but the function returns undefined. If I replace the return inside the callback with a console.log, that works fine. Why is this even though .every will return a true or false

function truthCheck(collection, pre) {
  // Is everyone being true?
  collection.every( coll => { return (Boolean(coll[pre]) );
	})
}

The other option I tried was the awkward looking code posted, where I assign the result of the callback to a variable and return that variable. Replacing return with console.log was another one . That worked

I’m trying to understand why we need the return before calling the .every on the array, when .every should return a false or true.


#7

Well that return is a return statement for truthCheck(). Without it the function has no return statement; thus, it always return undefined. It is not some weird quirk of .every().

function truthCheck(collection, pre) {
  
  // This return is for truthCheck(),
  // trutchCheck() returns the result of collection.every(..)
  return collection.every( coll => {
       // this return is for .every(..)'s callback
       return (Boolean(coll[pre]) )
  })
}

#8

every returns a value of true or false, but since you do not assign it to a variable and then return that variable, it does not get returned outside of the function.

If you do not explicitly return a value using the return keyword, then by default JavaScript will make the function returned the value undefined.

This is just a basic JavaScript concept. You might need a refresher onhow to return values from a function.


#9

Thanks ! That clears the gap in my understanding