Home > Blockchain >  How do i do this with JavaScript
How do i do this with JavaScript

Time:09-27

Im in a highschool coding course and think i've bit off more than i can chew. i need some help doing this homework i know i have to use .filter and .map i think but im not sure. its due tomorrow if anyone can help me id appreciate it. idk where else to go.

const nameNgrade = [
  { name: "Shaun", grade: 95 },
  { name: "Alex", grade: 65 },
  { name: "Nathan", grade: 87 },
];

1) Create a function that returns the grade

let v=  nameNgrade.map

2) Create a function that returns the name of a student with a grade less than 90.

3) Create a function that returns the average for all the grades

CodePudding user response:

To get the list of grades use map.

To get the names where the grade is less than 90, filter out those objects that match, and then map over that array to get the names.

For the sum, reduce over the array and add the grade of each iterated object to the initial value 0 which is passed into each iteration as the accumulator.

const nameNgrade = [
  { name: 'Shaun', grade: 95 },
  { name: 'Alex', grade: 65 },
  { name: 'Nathan', grade: 87 }
];

function getGradeList(arr) {
  return arr.map(obj => obj.grade);
}

function getNameForUnder90(arr) {
  const filtered = arr.filter(obj => obj.grade < 90);
  return filtered.map(obj => obj.name);
}

function getSum(arr) {
  return arr.reduce((acc, c) => acc   c.grade, 0);
}

console.log(getGradeList(nameNgrade));
console.log(getNameForUnder90(nameNgrade));
console.log(getSum(nameNgrade));

CodePudding user response:

1) map will just loop over the array and return a new array of values that you will return from it.

nameNgrade.map((o) => o.grade)

2) You can use filter and map to get all the name

nameNgrade.filter((o) => o.grade < 90).map((o) => o.name);

3) To get the average of all grades, First you have to add them and divide it by the length. You can use reduce here

nameNgrade.reduce((acc, curr) => acc   curr.grade, 0) / nameNgrade.length;

const nameNgrade = [
  { name: "Shaun", grade: 95 },
  { name: "Alex", grade: 65 },
  { name: "Nathan", grade: 87 },
];

const first = nameNgrade.map((o) => o.grade);
console.log(first);

const second = nameNgrade.filter((o) => o.grade < 90)
                         .map((o) => o.name);
console.log(second);

const third =
  nameNgrade.reduce((acc, curr) => acc   curr.grade, 0) / nameNgrade.length;
console.log(third);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

1.

Simply map through the array and get the grade property:

const nameNgrade = [
  { name: "Shaun", grade: 95 },
  { name: "Alex", grade: 65 },
  { name: "Nathan", grade: 87 },
];


const grades = nameNgrade.map(e => e.grade)

console.log(grades)

2.

Use Array.filter to filter out all items in the array where the grade property is bigger than 90.

If you want to get the names, map over the resulting array after filtering to extract the name property.

const nameNgrade = [
  { name: "Shaun", grade: 95 },
  { name: "Alex", grade: 65 },
  { name: "Nathan", grade: 87 },
];


const grades = nameNgrade.filter(e => e.grade < 90).map(e => e.name)

console.log(grades)

3.

You can sum the grades with Array.reduce by incrementing the accumulator by the grade property, then divide the result by the number of grades:

const nameNgrade = [
  { name: "Shaun", grade: 95 },
  { name: "Alex", grade: 65 },
  { name: "Nathan", grade: 87 },
];

const grades = nameNgrade.reduce((a,b) => a  = b.grade, 0)

const avg = grades / nameNgrade.length;

console.log(avg)

  • Related