1

let's suppose I have a class:

class Intern{
        constructor(name, mark) {
    this.name = name
    this.mark = mark
  }
    getMark() {return mark}
}

 let a = new Intern('John', 10) // let's say I create 3 objects of this class
 let b = new Intern('Marry', 9) 
 let c = new Intern('Anne', 8)

How I can pass an array of Interns as an argument to another class Mentor? I can create an empty array and do like this:

arr = []
 
Mentor{
   constructor(name, arr){
   this.name
   this.arr
  }
  getMentorHappiness() = this.arr.Intern.mark * 2 
}

Is that correct? So in dependent-ion on which mentor I select it will show me the only the get happiness method of these interns that only this Mentor teaches. Something like that:

let d = new Mentor('Dean', [Anne, John]
let e = new Mentor('Sam', [Marry])

I think I have to refer somehow to the info based on the Mentors array of objects input?

2
  • Your instances are called a, b, and c. Anne, John, and Marry do not exist. What is the purpose of arr = []? Commented Apr 6, 2022 at 4:40
  • Oh yes yes, a,b,c it's just mistype. I meant a,b,c. Well, I don't know how to pass the array of objects correctly. I thought I can create a table, so to pass it as an argument to the class, and then when a specific a or b is written in mentors class to add this a and b to the mentor's class constructor something like this, but I don't have idea how to do it :/ Commented Apr 6, 2022 at 4:46

1 Answer 1

2

Few things here:

  1. you forgot mentor is a class ( class Mentor )
  2. forgot to assign values from constructor
  3. forgot to return the value from getMentorHappiness function
  4. you need to pass an array with interns to the Mentor constructor, not an empty one
  5. you need then to address the intern array somehow to get the mark from it
  6. forgot to reference this. in Intern's mark to return

Below is the example of refactored your code, that works

class Intern{
    constructor(name, mark) {
    this.name = name;
    this.mark = mark;
  }
  getMark() {return this.mark;}
}

class Mentor{
    constructor(name, arr) {
    this.name = name;
    this.arr = arr;
  }
  getMentorHappiness() {
    let sum = 0; 
    for (let intern of this.arr) { 
      sum += intern.getMark();
    } 
    return sum / this.arr.length * 2 
  }
}

// driver code to test
let a = new Intern('John', 10);
let b = new Intern('Marry', 9);
let c = new Intern('Anne', 8);

let d = new Mentor('Dean', [a, b, c]);

console.log(d.getMentorHappiness());
console.log(a.getMark());
console.log(d.arr[0].getMark());

This function will calculate average for the Interns for the specific mentor. Please note it's multiplied by 2 as in the original question

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

4 Comments

I understand what you're saying, thank you, however if i need more interns to use in a single function since in this case function argument is only 1 intern. For example I need to make getHappiness an average of marks of the specific interns. let's say: let d = new Mentor('Dean', [a, c); so I have to calculate the average of these 2 interns, so I have to have an array to pass somehow. getHappiness() { let sum = 0; for (let i = 0; i < arr.length; i++) { sum = sum + this.arr.Intern.getMark();} return sum / this.arr.length} but it doesn't work
Amended the answer to accomodate for your question.
Thank you, it really helped I didn't know that for loop can be used this way: (let intern of this.arr) I tried to put it in a regular loop as of i=0 i<arr.length; i++ but it gave me some errors. However your method works fine. Thank you! Will learn further more.
The issue in your comment wasn't the loop itself, but the this.arr.Intern.getMark() part, that is invalid. If you addressed it by this.arr[i].getMark() it would work. Happy coding!

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.