Using an array inside a constructor using Javascript

13,964

Solution 1

You have a couple things messed up. If you are going to pass the grades array in as an argument, then you need to set grades with this:

this.grades = grades;

Also in the average function you need to refer to grades with this.grades not just grades. This will allow you to add more grades later and still get the correct average. You could also consider making the grades optional by defining the constructor with something like:

function student(name, surname, number, grades =[])

Then if you don't pass in a value, an empty array will be waiting for you.

In the end you might have something like:

function student(name, surname, number, grades = []) {
  this.name = name;
  this.surname = surname;
  this.number = number;
  this.grades = grades;
  this.average = function() {
    return this.grades.reduce((a, c) => a + c, 0) / this.grades.length
  }
}

var student1 = new student("Peter", "Cat", 14444, [2, 3, 4]);
console.log("Average: ", student1.average())

// add another grade:
student1.grades.push(6)
console.log("New Average: ", student1.average() )

Solution 2

You can solve same problem by using ES6, in your code, you are initializing this.grade=[] with an empty array inside function so further processing of average will be done on empty array only. For good practice, function parameters should be assigned with a default value, so that if mistakenly we do not pass an array as an argument then the function parameter will use the default value. Attaching code snippet for easy understanding in ES6.

class std{
constructor(name, surname, number, grades = []) {
    this.name = name;
    this.surname = surname;
    this.number = number;
    this.grades = grades;
}  

average() {
  if(this.grades.length !== 0){
    return this.grades.reduce((previous, current) => previous + current, 0) / 
    this.grades.length
  } else { return "no grades mentioned"; }
 }
}

var student1 = new std("Peter", "Cat", 14444, [1, 3, 4]);
console.log("Average: ", student1.average());
//add new student
var student2 = new std("Prasanna", "sasne", 14444);
console.log(student2); 

//student2.grades.push(7)
//console.log("New Average: ", student2.average() )

Solution 3

You're already passing grades into the student() function, so you don't need to pass it in to the student.average function (as the inner function will already have access to the outer function parameter). Because of this, you also don't need to set this.grades = [].

Also, sum + = grades[i] should be sum += grades[i].

Simply fixing this error, then omitting passing grades into the inner function will correctly show the average, as can be seen in the following:

function student(name, surname, number, grades) {
  this.name = name;
  this.surname = surname;
  this.number = number;
  this.average = function() {
    var sum = 0;
    for (var i = 0; i < grades.length; i++) {
      sum += grades[i];
    }
    var average = sum / grades.length;
    return average;
  }
}

var student1 = new student("Peter", "Cat", 14444, [2, 3, 4]);
console.log(student1.average());

Share:
13,964

Related videos on Youtube

Hoppi
Author by

Hoppi

Updated on June 04, 2022

Comments

  • Hoppi
    Hoppi about 2 years

    I've been trying to find a way to correctly define an array as one of the constructor values. Let's say we have a student and we need to have an array with his grades and then using the array we need to get an average from the student's grades. Sadly, I only found some threads addressing this in other programming languages. This is what I thought would work:

    function student(name, surname, number, grades) {
    this.name = name;
    this.surname = surname;
    this.number = number;
    this.grades = [];
    this.average = function(grades) {
     var sum = 0;
     for(var i = 0; i < grades.length; i++) {
      sum + = grades[i];}
      var average = sum / grades.length;
      return average;
     }
    }
    

    And then

    var student1 = new student("Peter","Cat",14444,[2,3,4]);
    console.log(student1);
    

    Unfortunately, it shows my grades array as blank and I can't see if my average function is working properly. Which part(s) should I change so that I would actually have some values in the grades array?

    Thank you.

    • John Montgomery
      John Montgomery about 6 years
      this.grades = []; Shouldn't that be this.grades = grades?