0

I'm making a javascript course and I'm trying to create a function that filters from the array and removes a given value of it. NOTE: I've to use the filter() method.

I've tried several ways but without success. What am I doing wrong?

Here the code:

function Student(id, name, subjects = []) {
  this.id = id;
  this.name = name;
  this.subjects = subjects;  
}

Student.prototype.addSubject = function(subject) {
  this.subjects = [...this.subjects, subject];   
}

Student.prototype.removeSubject = function(remove) {
    this.subjects.filter(function(remove) {
        return this.subjects !== remove
    })
}

const student1 = new Student(1, 'Reed');

student1.addSubject('Math');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)

Appreciate your time

3
  • filter doesn't modify the array, it returns a new one. this.subjects = this.subjects.filter(...) would fix the problem Commented Feb 25, 2022 at 10:23
  • Thanks for the response. I've added on the body of the function this.subjects = this.subjects.filter(function(remove)... but still get ['Math] when console.log Commented Feb 25, 2022 at 10:27
  • yes the filter needed some improvement. finally added as answer Commented Feb 25, 2022 at 10:27

2 Answers 2

2

filter doesn't modify the array, it returns a new one. this.subjects = this.subjects.filter plus a change in how you filter:

function Student(id, name, subjects = []) {
  this.id = id;
  this.name = name;
  this.subjects = subjects;  
}

Student.prototype.addSubject = function(subject) {
  this.subjects = [...this.subjects, subject];   
}

Student.prototype.removeSubject = function(remove) {
    this.subjects = this.subjects.filter(function(current) {
        return current !== remove
    })
}

const student1 = new Student(123,'pepe')

student1.addSubject('a');
student1.addSubject('Math');
student1.addSubject('b');
student1.addSubject('c');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)

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

1 Comment

Yes! that's it. Appreciate you took the time to answer me so fast.
1

You can use .push() and .splice() to modify the subjects array in-place, instead of re-creating it for every modification, which a) is a waste and b) can cause difficult-to-find bugs.

But you absolutely should create a new array at the point of object creation, to avoid inadvertently modifying the subjects parameter outside during the lifetime of the Student instance.

function Student(id, name, subjects = []) {
  this.id = id;
  this.name = name;
  if (!Array.isArray(subjects)) throw new Error("subjects must be an array");
  this.subjects = [...subjects];
}

Student.prototype.addSubject = function (subject) {
  this.subjects.push(subject);   
}

Student.prototype.removeSubject = function (subject) {
  const pos = this.subjects.indexOf(subject);
  if (pos > -1) this.subjects.splice(pos, 1);
}

const student1 = new Student(1, 'Reed');

student1.addSubject('Math');
console.log(student1.subjects);
student1.removeSubject('Math');
console.log(student1.subjects)

2 Comments

the problem is that OP needs to use .filter() . must be a req from the course they're taking
@malarres I was on the fence about that, but it could be.

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.