# Class Method Problem

**URL:** https://forum.kirupa.com/t/class-method-problem/652466
**Category:** web dev
**Created:** [June 30, 2022, 2:59pm UTC](https://forum.kirupa.com/t/class-method-problem/652466 "2022-06-30T14:59:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Zach\_Lipscomb](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/zach_lipscomb/32/17606_2.png) [@Zach\_Lipscomb](https://forum.kirupa.com/u/Zach_Lipscomb)
#### Post date: [June 30, 2022, 2:59pm UTC](https://forum.kirupa.com/t/class-method-problem/652466/1 "2022-06-30T14:59:38Z")

</div>

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

```auto
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', 'bugs@bunny.com');
console.log(testStudent);
if (
  testStudent.name === 'Bugs Bunny' &&
  testStudent.email === 'bugs@bunny.com'
) {
  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);

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [June 30, 2022, 7:21pm UTC](https://forum.kirupa.com/t/class-method-problem/652466/2 "2022-06-30T19:21:09Z")

</div>

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.

```auto
.
.
.
  } 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?

---

<div class="post-metadata">

### Author: ![Zach\_Lipscomb](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/zach_lipscomb/32/17606_2.png) [@Zach\_Lipscomb](https://forum.kirupa.com/u/Zach_Lipscomb)
#### Post date: [June 30, 2022, 8:06pm UTC](https://forum.kirupa.com/t/class-method-problem/652466/3 "2022-06-30T20:06:01Z")

</div>

Hi Kirupa,

Thanks for taking the time to help.  
Heres the link to codepen - [https://codepen.io/cryptozachary/pen/oNqgyyp](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…

```auto
  }

  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;

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [June 30, 2022, 8:27pm UTC](https://forum.kirupa.com/t/class-method-problem/652466/4 "2022-06-30T20:27:54Z")

</div>

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:

```auto
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 🙂

---

<div class="post-metadata">

### Author: ![Zach\_Lipscomb](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/zach_lipscomb/32/17606_2.png) [@Zach\_Lipscomb](https://forum.kirupa.com/u/Zach_Lipscomb)
#### Post date: [June 30, 2022, 10:46pm UTC](https://forum.kirupa.com/t/class-method-problem/652466/5 "2022-06-30T22:46:32Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [July 1, 2022, 4:41am UTC](https://forum.kirupa.com/t/class-method-problem/652466/6 "2022-07-01T04:41:41Z")

</div>

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](https://www.kirupa.com/javascript/sets.htm)

😀

---

<div class="post-metadata">

### Author: ![Zach\_Lipscomb](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/zach_lipscomb/32/17606_2.png) [@Zach\_Lipscomb](https://forum.kirupa.com/u/Zach_Lipscomb)
#### Post date: [July 1, 2022, 6:33am UTC](https://forum.kirupa.com/t/class-method-problem/652466/7 "2022-07-01T06:33:39Z")

</div>

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