Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active January 16, 2020 14:51
Show Gist options
  • Save ross-u/f222e5e4c2dc258a36af8bd2dc1464b6 to your computer and use it in GitHub Desktop.
Save ross-u/f222e5e4c2dc258a36af8bd2dc1464b6 to your computer and use it in GitHub Desktop.
JS | Array methods - map() - exercise

JS | Array methods - map()


Get the Final Grade - Exercise


img


For the following exercise you can use ES5 or ES6 syntax, whichever you feel more comfortable with at this point.

Go ahead to Repl.it , using the code below as your starting point.


Introduction:

Imagine you are a Math teacher and you have to grade your students based on their performance on three projects. We got the info for each student in an object that looks like this:

{
  name: "Student Name",
  firstProject: 80,
  secondProject: 75,
  finalProject: 90
}

Task:

The whole class is represented as an array of objects (each object contains the data about that student).

Using the method map() we need to create a new array of objects. The objects will contain only student´s name, and their final grade, which will be the new property named finalGrade.

finalGrade should be the average of the score that the student had on 3 projects.


The formula for the finalGrade should look like this :

(firstProject + secondProject + finalProject) / 3


Expected Result:

/*
0: {name: "Tony Parker", finalGrade: 82}
1: {name: "Marc Barchini", finalGrade: 71}
2: {name: "Claudia Lopez", finalGrade: 80}
3: {name: "Alvaro Briattore", finalGrade: 81}
4: {name: "Isabel Ortega", finalGrade: 69}
5: {name: "Francisco Martinez", finalGrade: 74}
6: {name: "Jorge Carrillo", finalGrade: 83}
7: {name: "Miguel López", finalGrade: 77}
8: {name: "Carolina Perez", finalGrade: 75}
9: {name: "Ruben Pardo", finalGrade: 75}
*/

Go ahead to Repl.it, using the code below as your starting point.

const students = [
{
name: "Tony Parker",
firstProject: 80,
secondProject: 75,
finalProject: 90
},
{
name: "Marc Barchini",
firstProject: 84,
secondProject: 65,
finalProject: 65
},
{
name: "Claudia Lopez",
firstProject: 45,
secondProject: 95,
finalProject: 99
},
{
name: "Alvaro Briattore",
firstProject: 82,
secondProject: 92,
finalProject: 70
},
{
name: "Isabel Ortega",
firstProject: 90,
secondProject: 32,
finalProject: 85
},
{
name: "Francisco Martinez",
firstProject: 90,
secondProject: 55,
finalProject: 78
},
{
name: "Jorge Carrillo",
firstProject: 83,
secondProject: 77,
finalProject: 90
},
{
name: "Miguel López",
firstProject: 80,
secondProject: 75,
finalProject: 75
},
{
name: "Carolina Perez",
firstProject: 85,
secondProject: 72,
finalProject: 67
},
{
name: "Ruben Pardo",
firstProject: 89,
secondProject: 72,
finalProject: 65
}
];
const finalGrades = students.map( /* Your Code Here */);
console.log(finalGrades);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment