Adriel Tosi
Adriel Tosi

Reputation: 291

Method .includes() is not a function error

I am studying through Eloquent Javascript, and I have the following code as one of the exercices.

class Group {
constructor(){
    this.arr = []
}

add(value){
    if(!this.has(value)) {
        this.arr = this.arr.push(value)
    }
}

has(value){
    return this.arr.includes(value);
}

delete(value){
    this.arr = this.arr.filter(n => n !== value)
}

static from(collection){
    let rec = new Group;
    for (let value of collection){
        rec.add(value)
        }
return rec
    }
}

It seems right and it should be working but I get the error

TypeError: arr.includes is not a function

What is that about? I can't find the answer.

Upvotes: 0

Views: 7983

Answers (1)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

From docs,

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Hence, the problem is with this line. this.arr is no longer an array after this line.

this.arr = this.arr.push(value)

So, update this to following

this.arr.push(value)

Upvotes: 2

Related Questions