Blog

Array

JS

Exercise

Array method exercise problems

Simple and effective exercise ๐Ÿ‹๏ธ to build your array muscles

Ragav Kumar V

Ragav Kumar V

Jan 13, 2023 โ€” Updated Jan 15, 2023 ยท 2 min read


_30
const scores = [
_30
{
_30
marks: 32,
_30
name: "Yvette Merritt",
_30
},
_30
{
_30
marks: 57,
_30
name: "Lillian Ellis",
_30
},
_30
{
_30
marks: 22,
_30
name: "Mccall Carter",
_30
},
_30
{
_30
marks: 21,
_30
name: "Pate Collier",
_30
},
_30
{
_30
marks: 91,
_30
name: "Debra Beard",
_30
},
_30
{
_30
marks: 75,
_30
name: "Nettie Hancock",
_30
},
_30
{
_30
marks: 20,
_30
name: "Hatfield Hodge",
_30
},
_30
];

Use the above data to answer all the below tasks

Print the name list - as an array


_9
[
_9
"Yvette Merritt",
_9
"Lillian Ellis",
_9
"Mccall Carter",
_9
"Pate Collier",
_9
"Debra Beard",
_9
"Nettie Hancock",
_9
"Hatfield Hodge",
_9
];

Answer

_1
scores.map(({ name }) => name);

Criteria for passing >=40. Find those students that passed test.


_14
[
_14
{
_14
marks: 57,
_14
name: "Lillian Ellis",
_14
},
_14
{
_14
marks: 91,
_14
name: "Debra Beard",
_14
},
_14
{
_14
marks: 75,
_14
name: "Nettie Hancock",
_14
},
_14
];

Answer

_1
scores.filter(({ marks }) => marks >= 40);

Find all the names who failed the exams


_1
["Yvette Merritt", "Mccall Carter", "Pate Collier", "Hatfield Hodge"];

Answer

_1
scores.filter(({ marks }) => marks < 40).map(({ name }) => name);

Find the Average marks

Answer

_3
const total = scores.reduce((sum, { marks }) => sum + marks, 0);
_3
_3
console.log(total, total / scores.length);

Find the topper's name


_1
"Debra Beard";

Answer

_5
const topper = scores.reduce((topper, student) =>
_5
topper.marks < student.marks ? student : topper
_5
);
_5
_5
console.log(topper.name);

@ragavkumarv
swipe to next โžก๏ธ