Strife_x7x
Strife_x7x

Reputation: 45

Call certain value and first name from list

Hello so first thanks in advance and I would like to say that I am more of a visual learner rather then reading through a text(would take me multiple times). I have this list in which I need to get the average of all the numbers in the "math" and then once I find the average, I need to pull the first name only of the below average number and print it. So here's the thing, I don't want the answer but a nudge in the right direction. Videos to youtube that would put me on the right path with this certain problem

var dataSet = [
{
    "name": "Maura Glass",
    "age": 60,
    "math": 97,
    "english": 63,
    "yearsOfEducation": 4
},
{
    "name": "James Gates",
    "age": 55,
    "math": 72,
    "english": 96,
    "yearsOfEducation": 10
},
{
    "name": "Mills Morris",
    "age": 26,
    "math": 83,
    "english": 77,
    "yearsOfEducation": 10
},
{
    "name": "Deena Morton",
    "age": 57,
    "math": 63,
    "english": 63,
    "yearsOfEducation": 10
},
{
    "name": "Edith Roth",
    "age": 38,
    "math": 79,
    "english": 94,
    "yearsOfEducation": 10
},
{
    "name": "Marva Morse",
    "age": 31,
    "math": 93,
    "english": 78,
    "yearsOfEducation": 9
},
{
    "name": "Etta Potts",
    "age": 48,
    "math": 57,
    "english": 93,
    "yearsOfEducation": 7
},
{
    "name": "Tate Moss",
    "age": 22,
    "math": 83,
    "english": 64,
    "yearsOfEducation": 8
},
{
    "name": "Sanders Burris",
    "age": 27,
    "math": 65,
    "english": 66,
    "yearsOfEducation": 5
},
{
    "name": "Latoya Malone",
    "age": 35,
    "math": 100,
    "english": 100,
    "yearsOfEducation": 5
},
{
    "name": "Wade Foreman",
    "age": 25,
    "math": 76,
    "english": 87,
    "yearsOfEducation": 10
},
{
    "name": "Miller Valentine",
    "age": 31,
    "math": 56,
    "english": 89,
    "yearsOfEducation": 6
},
{
    "name": "Rita Olson",
    "age": 53,
    "math": 100,
    "english": 52,
    "yearsOfEducation": 6
},
{
    "name": "Potter Newton",
    "age": 29,
    "math": 91,
    "english": 75,
    "yearsOfEducation": 5
}

]

I get the idea of getting the average, by gathering all the numbers in the "math" value, getting the sum and then dividing by how many values we have, in this case it'll be sum/14 = average # then I will create a function that'll pull the first name only of all below average numbers. Any ideas?

UPDATE!

So I manage to get the sum of all values in 'math' and then divide that to get the average score. I used

    function math(item) { //this should search for the value in math
    return item.math
}
    
function sum(prev, next) {
    return prev + next;
}
    
dataSet.map(math).reduce(sum);
    
    console.log(dataSet.map(math).reduce(sum)); //this gives total amount of all math scores
    console.log('Average score: ', dataSet.map(math).reduce(sum) / 27); //here we get average score between the 27 students

    var average = dataSet.map(math).reduce(sum) / 27 //this holds the average score

UPDATE 2

        let mathBelow = dataSet.filter(function(name) { //this displays names with equal and/or below average score
        if(name.math <= average) {
            return true;
        }
    });

I've manage to get it to display students with equal/below average score. This is as close as I can get before having to turn in the assignment. I did have this code set, but it gives me a FALSE and TRUE instead of a numerical number next to the name

        let testName = dataSet.map(function(names) {
        return `${names.name} [${names.math <= average}]`;
    });

I feel like it's there, almost done. Is the code above in the right direction? Am I missing something in order for it to just show the names of those with equal/below average score?

Even though I turned in the assignment, I still want to figure this out.

Upvotes: 1

Views: 53

Answers (1)

ElectricShadow
ElectricShadow

Reputation: 690

You said you didn't want the answer, so I won't give it to you, but here is a bit of logic to get you started:

sum = 0
loop through each student in dataSet:
  add student.math to sum
average = sum / (length of dataSet)

belowAverageStudents = []
loop through each student in dataSet:
  if student.math < average:
    firstName = first element of student.name after being split by ' '
    add firstName to belowAverageStudents

Hint for splitting the name: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

Upvotes: 1

Related Questions