Home > front end >  How to calculate the average in JavaScript of the given properties in the array of objects
How to calculate the average in JavaScript of the given properties in the array of objects

Time:01-03

I'm trying to get the average of the property "markAv" from the variable avengers.

avengers = []

function Avenger(fullName, classRoom, city, job, studies, markAv) {
    this.fullName =  fullName;
    this.classRoom = classRoom;
    this.city = city;
    this.job = job;
    this.studies = studies;
    this.markAv = markAv;
    avengers.push(arguments)
}

Avenger({
    fullName: "Hulk Paul", 
    classRoom: "V", 
    city: "Miami", 
    job: "Cientist", 
    studies: "Harvard", 
    markAv: 8
})

Avenger({
    fullName: "Tony Stark", 
    classRoom: "XI", 
    city: "NYC", 
    job: "Ingeneer", 
    studies: "MIT", 
    markAv: 10
})

Avenger({
    fullName: "Diana Princess", 
    classRoom: "III", 
    city: "NYC", 
    job: "Warrior", 
    studies: "Atenas", 
    markAv: 11
})

Avenger({
    fullName: "Thais Jacob", 
    classRoom: "IV", 
    city: "Málaga", 
    job: "IT", 
    studies: "Brazil", 
    markAv: 5
})

To get it, I inserted the function "getAverage". However, it is returning "NaN". I would like to know the reason and an explanation of how to solve it, because I don't understand why.


let getAverage = arr => {
    let reducer = (total, currentValue) => total   currentValue;
    let sum = arr.reduce(reducer)
    return sum / arr.length;
}
  
let average = avengers.map(markAv => avengers.markAv);
  
console.log(getAverage(average));

CodePudding user response:

There are several issues in your code:

  1. You are creating an array of arguments passed to the Avenger function, instead of creating an instance of the Avenger function constructor for each object.
  2. You are passing a object to the Avenger function instead of individual arguments.
  3. You are trying to access the markAv property of the avengers array instead of the current object being mapped over.

To fix these issues, you can do the following:

  1. Create an instance of the Avenger function for each object, using the new keyword and passing the required arguments.
  2. Pass the individual arguments to the Avenger function, not an object.
  3. Use the current object being mapped over (obj in this case) to access its properties, not the avengers array.

Here's the updated working code:

let avengers = [];

function Avenger(fullName, classRoom, city, job, studies, markAv) {
    this.fullName =  fullName;
    this.classRoom = classRoom;
    this.city = city;
    this.job = job;
    this.studies = studies;
    this.markAv = markAv;
}

avengers.push(new Avenger("Hulk Paul", "V", "Miami", "Cientist", "Harvard", 8));
avengers.push(new Avenger("Tony Stark", "XI", "NYC", "Ingeneer", "MIT", 10));
avengers.push(new Avenger("Diana Princess", "III", "NYC", "Warrior", "Atenas", 11));
avengers.push(new Avenger("Thais Jacob", "IV", "Málaga", "IT", "Brazil", 5));

let getAverage = arr => {
    let reducer = (total, currentValue) => total   currentValue;
    let sum = arr.reduce(reducer)
    return sum / arr.length;
}
  
let average = avengers.map(obj => obj.markAv);
  
console.log(getAverage(average));

CodePudding user response:

change let average = avengers.map(markAv => markAv.markAv); also please replace avengers.push(arguments[0])

avengers = []

function Avenger(fullName, classRoom, city, job, studies, markAv) {
  this.fullName = fullName;
  this.classRoom = classRoom;
  this.city = city;
  this.job = job;
  this.studies = studies;
  this.markAv = markAv;
  avengers.push(arguments[0]); // here changed
}

Avenger({
  fullName: "Hulk Paul",
  classRoom: "V",
  city: "Miami",
  job: "Cientist",
  studies: "Harvard",
  markAv: 8
})

Avenger({
  fullName: "Tony Stark",
  classRoom: "XI",
  city: "NYC",
  job: "Ingeneer",
  studies: "MIT",
  markAv: 10
})

Avenger({
  fullName: "Diana Princess",
  classRoom: "III",
  city: "NYC",
  job: "Warrior",
  studies: "Atenas",
  markAv: 11
})

Avenger({
  fullName: "Thais Jacob",
  classRoom: "IV",
  city: "Málaga",
  job: "IT",
  studies: "Brazil",
  markAv: 5
})

let getAverage = arr => {
  let reducer = (total, currentValue) => total   currentValue;
  let sum = arr.reduce(reducer)
  return sum / arr.length;
}

let average = avengers.map(item => item.markAv); // here changed

console.log(getAverage(average));

CodePudding user response:

It looks like you're trying to use a function constructor so you should be using the new keyword, and adding those new object instances to the array.

Then in your reducer add up all the markAv values, and then divide by the length of the array.

const avengers = [];

// Since you're passing in an object
// you can simply iterate over it and apply
// each prop value to `this` instead.
function Avenger(obj) {
  for (const prop in obj) {
    this[prop] = obj[prop];
  }
}

avengers.push(new Avenger({
  fullName: "Hulk Paul", 
  classRoom: "V", 
  city: "Miami", 
  job: "Cientist", 
  studies: "Harvard", 
  markAv: 8
}));

avengers.push(new Avenger({
  fullName: "Tony Stark", 
  classRoom: "XI", 
  city: "NYC", 
  job: "Ingeneer", 
  studies: "MIT", 
  markAv: 10
}));

avengers.push(new Avenger({
  fullName: "Diana Princess", 
  classRoom: "III", 
  city: "NYC", 
  job: "Warrior", 
  studies: "Atenas", 
  markAv: 11
}));

avengers.push(new Avenger({
  fullName: "Thais Jacob", 
  classRoom: "IV", 
  city: "Málaga", 
  job: "IT", 
  studies: "Brazil", 
  markAv: 5
}));

function getAverage(arr) {
  return arr.reduce((acc, c) => {
    return acc   c.markAv;
  }, 0) / arr.length;
}

console.log(getAverage(avengers));

CodePudding user response:

let avengers = []

function Avenger(fullName, classRoom, city, job, studies, markAv) {
    this.fullName =  fullName;
    this.classRoom = classRoom;
    this.city = city;
    this.job = job;
    this.studies = studies;
    this.markAv = markAv;
}

avengers.push(new Avenger("Hulk Paul", "V", "Miami", "Cientist", "Harvard", 8));
avengers.push(new Avenger("Tony Stark", "XI", "NYC", "Ingeneer", "MIT", 10));
avengers.push(new Avenger("Diana Princess", "III", "NYC", "Warrior", "Atenas", 11));
avengers.push(new Avenger("Thais Jacob", "IV", "Málaga", "IT", "Brazil", 5));

let Avg=avengers.reduce((cur,acc)=>cur acc.markAv,0)
console.log(Avg/avengers.length)

CodePudding user response:

const markSum = Avengers.reduce((sum, Avenger) => sum   Avenger.markAv, 0);
const markAvg = markSum / Avengers.length;

console.log(markAvg);
  • Related