0
var each = function(collection, callback){
    if(Array.isArray(collection)){
        for(var i=0;i<collection.length;i++){
            callback(collection[i]);
        }
    }else{
        for(var key in collection){
            callback(collection[key]);
        }
    }
};


var filter = function(collection, callback){
    each(collection, function(item){
        if(callback(item)){
            return item;
        }
   });
};

How to write a callback function that would output each boolean value of the callback?

// example, even of an array, [1,2,3,4,5] -> false, true, false, true, false

4 Answers 4

1
  • Within the function filter is missing the newArray with the filtered items.
  • This is an alternative:
var result = filter([1,2,3,4,5], function(n) {
    return n % 2 === 0;
});

var each = function(collection, callback) {
  if (Array.isArray(collection)) {
    for (var i = 0; i < collection.length; i++) {
      callback(collection[i]);
    }
  } else {
    for (var key in collection) {
      callback(collection[key]);
    }
  }
};

var filter = function(collection, callback) {
  var newArray = [];
  each(collection, function(item) {
    if (callback(item)) newArray.push(item);
  });

  return newArray;
};

var result = filter([1, 2, 3, 4, 5], function(n) {
  return n % 2 === 0;
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

If you want to save whether the callback returned true or false for each element, you need to store the single return values in a new array (as Ele already suggested);

var each = function(collection, callback) {
  if (Array.isArray(collection)) {
    for (var i = 0; i < collection.length; i++) {
      callback(collection[i]);
    }
  } else {
    for (var key in collection) {
      callback(collection[key]);
    }
  }
};

var filter = function(collection, callback) {
  var newArray = [];
  each(collection, function(item) {
    newArray.push(callback(item));
  });
  
  return newArray;
};

var source = [1,2,3,4,5];
var result = filter(source, function(n) {
  return n % 2 === 0;
});

console.log("source array", source);
console.log("result array", result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

const source = [1,2,3,4,5];
const isEven = n => n%2 === 0;

const result = source.map(el=>isEven(el));
console.log("source array", source);
console.log("result array", result);

Comments

0

I am not sure where you want to use this: in a browser? Old Scripting environment? Node? but it seems to me you are trying to re-invent the wheel here.

The Javascript builtin array functions include the Find, Foreach and Map. I am not sure how you want the output either, so I'll just log it.

In this case you can use Foreach In long hand..

    var nums=[1,2,3,4,5];
    nums.forEach(function(element) {
        console.log(element%2==0)
    });

which will output

false
true
false
true
false

If you are familiar with arrow functions this is even simpler

    var nums=[1,2,3,4,5];
    nums.forEach(element => {console.log(element%2==0)});

If you want to do this asynchronously, then you can wrap in a promise in most modern environments

    var nums=[1,2,3,4,5];
    var x= new Promise( (resolve) => {
            var result=[];
            nums.forEach(function(element) {
                result.push(element%2==0);
            });
            resolve(result);
            })
        .then(evens=>console.log(evens));

Comments

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.