1

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
})

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));
3
  • let average = avengers.map(avenger=> avenger.markAv); Commented Jan 3, 2023 at 9:12
  • 1
    Don't use global variables inside the constructor function. Remove the avengers.push(arguments) line. Use the new operator to create instances of Avenger and push those objects inside the avengers array. avengers.push(new Avenger("Tony Stark", "XI"....)) <- Also, don't pass an object. Just pass the values as arguments. Commented Jan 3, 2023 at 9:30
  • 1
    If you do want to pass an object as an argument, you can destructure the properties: function Avenger({ fullName, classRoom, city, job, studies, markAv }) Commented Jan 3, 2023 at 9:35

4 Answers 4

3

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));

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

1 Comment

Your explanation was very clear, I had not paid attention to these other items you mentioned. Thank you!
0

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));

1 Comment

That's really confusing to keep the same parameter names as the OP. Also, this.fullName is meaningless without the new operator.
0

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));

1 Comment

That loop can be replaced with: Object.assign(this, obj) (But, it might be confusing for OP. And, it looks like they are trying pass named arguments like C#)
0

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)

1 Comment

Instead of just pasting some code, explain how it works and the issue it solves.

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.