Skip to content

Commit 88a6f2d

Browse files
committed
#22 - JavaScript
1 parent 63c9620 commit 88a6f2d

File tree

1 file changed

+75
-0
lines changed
  • Roadmap/22 - FUNCIONES DE ORDEN SUPERIOR/javascript

1 file changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Function that accepts another function as an argument
2+
function processArray(func, arr) {
3+
return arr.map(func);
4+
}
5+
6+
function square(x) {
7+
return x * x;
8+
}
9+
10+
const numbers = [1, 2, 3, 4];
11+
const squares = processArray(square, numbers);
12+
console.log(squares);
13+
14+
// Function that returns another function
15+
function createSum(x) {
16+
return function(y) {
17+
return x + y;
18+
};
19+
}
20+
21+
const sum5 = createSum(5);
22+
console.log(sum5(20));
23+
24+
// Using filter, map and reduce
25+
const numbers10 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
26+
const evenNumbers = numbers10.filter(num => num % 2 === 0);
27+
const evenSquares = evenNumbers.map(num => num * num);
28+
const sumEvenSquares = evenSquares.reduce((total, num) => total + num, 0);
29+
30+
console.log(evenNumbers);
31+
console.log(evenSquares);
32+
console.log(sumEvenSquares);
33+
34+
// Extra Exercise //
35+
36+
const students = [{
37+
name: 'Isaac',
38+
birthdate: new Date(2001, 9, 21),
39+
qualifications: [9.0, 10, 9.5, 9.8]
40+
},
41+
{
42+
name: 'Juan',
43+
birthdate: new Date(2001, 6, 22),
44+
qualifications: [9, 8, 8.5, 9.5]
45+
},
46+
{
47+
name: 'Luis',
48+
birthdate: new Date(2003, 10, 5),
49+
qualifications: [6, 7, 7.5, 8]
50+
},
51+
{
52+
name: 'María',
53+
birthdate: new Date(2000, 4, 20),
54+
qualifications: [10, 9.5, 10, 10]
55+
}];
56+
57+
const studentAverage = students.map(student => {
58+
const average = student.qualifications.reduce((sum, score) => sum + score, 0) / student.qualifications.length;
59+
return { name: student.name, average: average.toFixed(2) };
60+
});
61+
console.log(studentAverage);
62+
63+
const bestStudents = studentAverage.filter(student => student.average >= 9);
64+
console.log(bestStudents);
65+
66+
const datesOfBirthInOrder = students.sort((a, b) => b.birthdate - a.birthdate);
67+
datesOfBirthInOrder.forEach(student => {
68+
const date = student.birthdate;
69+
console.log(`\n${student.name}: ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`);
70+
});
71+
72+
const allQualifications = students.flatMap(students => students.qualifications);
73+
const highestScore = Math.max(...allQualifications);
74+
75+
console.log(`\nThe highest score is ${highestScore}`);

0 commit comments

Comments
 (0)