Home > database >  Make one function accept parameters from the other
Make one function accept parameters from the other

Time:01-08

I have this task:

Write constructor functions that create objects representing students - they are to include first name, last name, index number, table with grades and a method that prints first name, surname and grade average. Use it to create an object that represents yourself.

So from my understanding, I need to create two constructor functions: one should be a functions accepting parameters for the grades from specific subject and calculate the average grade from a specific grade set of a specific student.

I'm trying to understand how to pas something like this

function Grade(math, compsc, sc){
    this.math = math;
    this.compcs = compsc;
    this.sc = sc;
}

Into something like this (or make it reference it, or somehow to pass the arguments of those predefined grade parameters values into this function)

function Student(index, first, last, grade) {
    this.indexNum = index;
    this.firstName = first;
    this.lastName = last;
    this.grade = grade;
    this.gradeAvg = function gradeAvg() {
        let total = 0;
        for (let value in this.grade) {
          total  = this.grade[value]
        }
        return total / Object.entries(this.grade).length;
    };
    this.outPrint = function outPrint() {
        console.log("Student number: "   JSON.stringify(this.indexNum)   "\n First name: "   this.firstName   "\n Last name: "   this.lastName   "\n Grades: \n Math:"   this.grade.math   "\n compcs:"  this.grade.compcs   "\n sc:"   this.grade.sc   "\n average grade:"  this.gradeAvg())
    }
}

Ideally, it would be something like this but a working one:

function Student(index, first, last, grade) {
    this.indexNum = index;
    this.firstName = first;
    this.lastName = last;
    grade = function(math, copcsc, sc){
        this.math = math;
        this.compsc = compcs;
        this.sc = sc;
    };
    this.gradeAvg = function gradeAvg() {
        let total = 0;
        for (let value in this.grade) {
          total  = this.grade[value]
        }
        return total / Object.entries(this.grade).length;
    };
    this.outPrint = function outPrint() {
        console.log("Student number: "   JSON.stringify(this.indexNum)   "\n First name: "   this.firstName   "\n Last name: "   this.lastName   "\n Grades: \n Math:"   this.grade.math   "\n compcs:"  this.grade.compcs   "\n sc:"   this.grade.sc   "\n average grade:"  this.gradeAvg())
    }
}

and then initialization

    const me = new Student (345345, 'jora', 'orlovskiy',{math:10, compcs: 10, sc: 5})
console.log(me.outPrint());

and with the grade

const meGr = new grade(2,2,2)

I understand that my mistake may be a stupid one with syntaxes or with understanding the basic concepts. Maybe the initialization is wrong but I tried everythingg to make it work in different ways, but nothing helped, would appreciate any help

CodePudding user response:

Use your original version of Student().

Then call new Grade() in the arguments to new Student().

const me = new Student (345345, 'jora', 'orlovskiy', new Grade(10, 10, 5));
  •  Tags:  
  • Related