Class Method Problem

Hello all - the bootcamp class registerStudent() method is not behaving as i expect. In this method, i am checking if the studentToRegister argument contains both a name and email , if so, i want to push the argument into the students array ONLY if there are bo duplicate email or names and if both name and emails are truthy. Been workkng this for two days and I cant figure out the problem. I appreciate your help and guidance.

Zachary

class Student {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }
}

class Bootcamp {
  constructor(name, level, students = []) {
    this.name = name;
    this.level = level;
    this.students = students;
  }

  registerStudent(studentToRegister) {
   

    if (!studentToRegister.name || !studentToRegister.email) {
      console.log('invalid name or email');
      return false
      
    } else {
this.students.push(studentToRegister);
      this.students.forEach((item, index, array) => {
if (item.email !== studentToRegister.email) {

          console.log(
            `Success : Thank you for registering ${studentToRegister.name} at ${this.name}`
          );
          return true;
        } else {
          console.log('Student already registered');
          return false;
        }
      });
    }
  }
}

testStudent = new Student('Bugs Bunny', '[email protected]');
console.log(testStudent);
if (
  testStudent.name === 'Bugs Bunny' &&
  testStudent.email === '[email protected]'
) {
  console.log('TASK 1: PASS');
}

reactBootcamp = new Bootcamp('React', 'Advanced');
console.log(reactBootcamp);
if (
  reactBootcamp.name === 'React' &&
  reactBootcamp.level === 'Advanced' &&
  Array.isArray(reactBootcamp.students) &&
  reactBootcamp.students.length === 0
) {
  console.log('TASK 2: PASS');
}

const runTest = (bootcamp, student) => {
  const attemptOne = bootcamp.registerStudent(student);
  const attemptTwo = bootcamp.registerStudent(student);
  const attemptThree = bootcamp.registerStudent(new Student('Babs Bunny'));
  if (attemptOne && !attemptTwo && !attemptThree) {
    console.log('TASK 3: PASS');
  }
};

runTest(reactBootcamp, testStudent);

Hi Zach - is the behavior for registeredStudent to be as follows:

  • If student name or e-mail is missing, don’t register student
  • If the student we are adding has an e-mail address that has already been used by another student, don’t register student

Is that right? If so, the logic in the “else” part of your code needs to be adjusted. Right now, your logic adds a student every time as long as they have a value for name and e-mail defined.

.
.
.
  } else {
    this.students.push(studentToRegister); // This line right here shouldn't be here?
    this.students.forEach((item, index, array) => {
      if (item.email !== studentToRegister.email) {
        console.log(
          `Success : Thank you for registering ${studentToRegister.name} at ${this.name}`
        );
        return true;
      } else {
        console.log('Student already registered');
        return false;
      }
    });
  }
}

You should add the student only if the results of the loop indicate that the student’s e-mail address hasn’t been set.

Does this help?

Hi Kirupa,

Thanks for taking the time to help.
Heres the link to codepen - https://codepen.io/cryptozachary/pen/oNqgyyp

I modified the code to slip the push line under the if statement - but its not working. It seems im trying to iterate of the array before the items are pushed into it. Hmm…

  }

  registerStudent(studentToRegister) {

   

    if (!studentToRegister.name || !studentToRegister.email) {

      console.log('invalid name or email');

      return false

      

    } else {
  

      this.students.forEach((item, index, array) => {

     

if (item.email !== studentToRegister.email) {
this.students.push(studentToRegister)
          console.log(

            `Success : Thank you for registering ${studentToRegister.name} at ${this.name}`

          );

          return true;

        } else {

          console.log('Student already registered');

          return false;

Take a look at my variation here where I use a variable called unique to determine whether the student is still unique by the time the loop has run to completion:

class Bootcamp {
  constructor(name, level, students = []) {
    this.name = name;
    this.level = level;
    this.students = students;
  }

  registerStudent(studentToRegister) {
    if (!studentToRegister.name || !studentToRegister.email) {
      console.log('invalid name or email');
      return false
    } else {
      let unique = true;

      this.students.forEach((item, index, array) => {
        if (item.email !== studentToRegister.email) {
          unique = true;
          console.log(
            `No e-mail match`
          );
        } else {
          unique = false;
          console.log('Email match');
        }
      });

      if (unique == true) {
        this.students.push(studentToRegister);
        console.log("Adding new student!");
        return true;
      } else {
        console.log("Not a unique student");
        return false;
      }
    }
  }
}

At the end of the loop, we can use the value of unique to determine whether we want to add a student or not. Now, there are ways to optimize this to avoid you having to loop through each item in the array each time, but we can cross that bridge once you feel comfortable with this approach first :slight_smile:

Wow. The simple power of booleans. I don’t know why I didnt think of that! Thank you.

Is there something that usually prompts you to use a boolean to solve a problem? Or some kind of fact about booleans or that helps you determine when to use one?

That is a tough one to answer! My general thinking is if there is a single right or wrong answer AND there are many steps required to figure out the answer, having a boolean keep track of the answer through each step makes sense.

For your code, one thing that would simplify your approach is to use Sets to store the student email IDs: Diving Into Sets

:grinning:

Understood! Great answer :gem: Definitely going to challenge myself to rewrite using sets as well. Judging from your video, it doesn’t look too difficult. :slight_smile:

1 Like