0

Hello I'm running some tests to see if the output of both is true. The thing is that given these conditions:

var callback = function (num) {
	return num % 2 === 0
}

console.log($$$.any([1, 3, 4], callback) === true)
console.log($$$.any([1, 11, 111], callback) === false)

I have to create a function that gives as a result in both cases true. I did it with a for loop but I can't get the same result with a forEach, why is that?:

// My for loop function:

$$$ = {
		any: function (arr, callback) {
		for (var i = 0, l = arr.length; i < l; i ++) {
				if (callback(arr[i])) {
				return true;
				}
			}
		return false;
		}
}

// My forEach function:

$$$={
	any: function(array, callback){
		array.forEach(function(value){
			// console.log(value)
			if (callback(value)) {
				return true;
			} 
			// return false;
		})
		return false;
	}
}

2
  • Use a debugger to walk through your program step by step. Commented Jan 20, 2017 at 11:14
  • whoever just used the "cannot be reproduced error / typo" close reason - you're wrong. This is a logic error. Commented Jan 20, 2017 at 11:32

1 Answer 1

2

In your second version, the return true in the inner callback function only returns from that callback function, and doesn't cause the outer function to return.

The .forEach loop will run to completion, and then the .any function itself always returns false.

FWIW, .forEach isn't really suitable for a .any function because there's no way to terminate the iteration over the elements as soon as a match is found, and it's therefore less efficient than it could be. For example, you might have an array with 10000 elements, but using .forEach it'll keep on looping, even if the very first element satisfied your predicate.

That said, if you really must use .forEach, though, the solution would be:

function all(array, callback) {
    var result = false;
    array.forEach(function(value) {
        if (callback(value)) {
            result = true;
        }
    });
    return result;
}

with a further optimisation possible to at least avoid calling the user supplied callback once a match is found:

function all(array, callback) {
    var result = false;
    array.forEach(function(value) {
        if (!result && callback(value)) {
            result = true;
        }
    });
    return result;
}

NB: JavaScript already has a function that does what you require - Array.prototype.some

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so there's no way to get the same result with a forEach?
@Defoe there is, by having a variable that is initially set to false in the outer scope that's then set to true inside the callback if the predicate matches, and then returning that variable. You shouldn't, though, for the efficiency reasons I outlined above.
Ok @Alnitak I see, I would use the some function in other cases. Could you please put your last suggestion of the forEach solution in an answer so I can validate it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.