Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
JavaScript filter WITHOUT .filter function

I am trying to write a function that will filter an array WITHOUT using the .filter function. Here is the function as I've written it so far;

function filter(ray, fn) {
    //The easy way
    //let filterArray = ray.filter(fn);
    //return filterArray;

    let filterArray = [];
    for (let i = 0; i < ray.length; ++i) {
        if (fn(i) === true) {
            filterArray.push(i);
        } else {
            //do nothing            
        }
    }
    return filterArray;
}

The functions used as fn are;

function isOdd(x) {
    return x % 2 === 1;
}  
function alwaysTrue(x) {
    return true;
}  
function alwaysFalse(x) {
    return false;
}

The function currently works with the alwaysFalse function, but not the other two. Where am I going wrong?

function filter(ray, fn) {
    //The easy way
    //let filterArray = ray.filter(fn);
    //return filterArray;

    let filterArray = [];
    for (let i = 0; i < ray.length; ++i) {
        if (fn(i) === true) {
            filterArray.push(i);
        } else {
            //do nothing            
        }
    }
    return filterArray;
}

function isOdd(x) {
    return x % 2 === 1;
}  
function alwaysTrue(x) {
    return true;
}  
function alwaysFalse(x) {
    return false;
}

console.log(filter([1,2,3,4], isOdd)); // [1,3]
console.log(filter([1,2,3,4], alwaysFalse)); // []

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You're checking fn(i), where i is the index of the for loop. You should be checking fn(ray[i]), or the value of the array at the given index. Same goes for the push -- you should push ray[i], not i.

function filter(ray, fn) {
    //The easy way
    //let filterArray = ray.filter(fn);
    //return filterArray;

    let filterArray = [];
    for (let i = 0; i < ray.length; ++i) {
        if (fn(ray[i]) === true) {
            filterArray.push(ray[i]);
        } else {
            //do nothing            
        }
    }
    return filterArray;
}

function isOdd(x) {
    return x % 2 === 1;
}  
function alwaysTrue(x) {
    return true;
}  
function alwaysFalse(x) {
    return false;
}

const arr = [1, 2, 3, 4, 5, 6, 7];
console.log(filter(arr, isOdd));

about 4 years ago · Juan Pablo Isaza Report

0

You're pushing the index not the element

filterArray.push(i);
// should be
filterArray.push(ray[i]);

You're also calling the function on the index not the element

if (fn(i) === true) {
// should be
if (fn(ray[i]) === true) {

Other than that your code is a-okay.

about 4 years ago · Juan Pablo Isaza Report

0

You can easily loop through the array using for...of and push the item into a new array.

Note: The filter callback functions should return true if a condition is met.

function filter(arr, fn) {
  const filtered = [];
  for(const item of arr){
    if(fn(item)) filtered.push(item);
  }
  return filtered;
}

const isOdd = (x) =>  x % 2 === 1;

const numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(filter(numbers, isOdd));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!