0

If I call the filter function, I get this array returned [ 1, , 3, , 5 ]. From where come the additional commas? I don't understand this effect. Can somebody explain it to me? The array should be that: [ 1, 3, 5 ].

class List {
    constructor(values = []) {
        this._list = values;
    }

    
	filter(func) {
		let newList = new Array();
		let indexList = 0;
		let indexNewList = 0;

		while (this._list[indexList] != undefined) {
			if (func(this._list[indexList]) === true) {
				newList[indexNewList] = this._list[indexList];
				indexNewList++;
			}
			indexList++;
		}
		this._list = newList;
		return this;
	}
  
  get values() { return this._list }
}

var isOdd = function (x) {
    return x % 2 === 1;
};

var list = new List([1, 2, 3, 4, 5]);
console.log(list.filter(isOdd).values);

3 Answers 3

1

If an item in the list matches the filter, you're inserting it into the new list at the index of the item in the original list. You want to simply be appending the item to the new list.

Use another variable to keep track of what index the element should be inserted into the new list at:

let newList = new Array();
let indexList = 0;
let newIndex = 0;

while (this._list[indexList] != undefined) {
    if (func(this._list[indexList]) === true) {
        newList[newIndex] = this._list[indexList];
        newIndex++;
    }
    indexList++;
}

The newIndex variable will only be incremented when an item has been inserted into newList, instead of being incremented with every iteration of the loop.

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

4 Comments

no i cant use push, i have to program it without using existing functions. But you got me to the right solution. I didn't recognize that i need a second index counter. Thanks! I changed it above now it is working!
In that case, set the element at the index that is the length of the array. I've updated my answer.
But length is also an existing function
Use another variable to keep track of the index to insert into the new list at. I've updated my answer.
0

The problem is the increment of the variable index, that increment is creating empty/undefined elements.

For example:

  • Array = [1];
  • index = 1
  • callback returns false
  • The index is incremented by 1 -> index =2`
  • Next iteration callback returns true
  • A new element is added to Array at position 2 ->
  • Array = [1, undefined, 3].

Use a separated index for the newArray.

class List {
  constructor(values = []) {
    this._list = values;
  }

  filter(func) {
    let newList = new Array();
    let index = 0;
    let newListIndex = 0;

    while (this._list[index] != undefined) {
      if (func(this._list[index]) === true) {
        newList[newListIndex++] = (this._list[index]);
      }
      index++;
    }
    this._list = newList;
    return this;
  }

  get values() {
    return this._list
  }
}

var isOdd = function(x) {
  return x % 2 === 1;
};

var list = new List([1, 2, 3, 4, 5]);
console.log(list.filter(isOdd));

Comments

0

I would suggest sticking with a functional-programming style definition of array#filter since that is the paradigm it originates from.

This is the fully immutable version of List#filter where you get a new instance of List back and the underlying array never goes through any form of mutation.

class List {
  constructor(values = []) {
    this._list = values;
  }

  filter(func) {
    var reduce = function(values, accum) {
      if(values.length === 0) return accum;
      if(func(values[0])) return reduce(values.slice(1), accum.concat(values[0]));
      else return reduce(values.slice(1), accum)
    }
    return new List(reduce(this._list, []))
  }

  get values() {
    return this._list
  }
}

var isOdd = function(x) {
  return x % 2 === 1;
};

var list = new List([1, 2, 3, 4, 5]);
console.log(list.filter(isOdd));

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.