Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I Need math marks > 50 then it will show otherwise not show in given JavaScript program

Here I am not able to hide the lower mark field

let result = [];
var Avgmarks = 0,i;
let students = [{name: "Student1",sub: {math: 80,eng: 50,science: 70}},{name: "Student2",sub:{math: 40,eng: 65,science: 85}}];

students.forEach( student => {
  result.push({
    name: student.name,
    Subject: student.sub,
    marks: Object.values(student.sub).reduce((a, b) => a + b),
    
    Average:  ( Object.values(student.sub).reduce((a, b) => (a + b)) ) / ( Object.values(student.sub).length )
    
  })
});

console.log(result)

[
{
name: ‘Student1’,
Subject: { math: 80, eng: 50, science: 70 },
marks: 200,
Average: 120
},
{
name: ‘Student2’,
Subject: { math: 40, eng: 65, science: 85 },
marks: 190,
Average: 90
}
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the way you are calculating average using reduce is wrong

if you know there is going to be 3 subjects always.. you can try

 { Average: Object.values(student.sub).reduce((a, b) => (a + b))/3 }

if subjects length can vary

Average:  ( Object.values(student.sub).reduce((a, b) => (a + b)) ) / ( Object.values(student.sub).length ) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading