0

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)); // []

0

3 Answers 3

3

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));

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

1 Comment

Oh man, that makes total sense now. Thank you so much for clarifying that for me!
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.

Comments

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));

1 Comment

That's really slick! I'm still beefing up my JS skills so it's nice to see different ways to do the same thing!

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.