|
| 1 | +// Function that accepts another function as an argument |
| 2 | +function processArray<T>(func: (item: T) => T, arr: T[]): T[] { |
| 3 | + return arr.map(func); |
| 4 | +} |
| 5 | + |
| 6 | +function square(x: number): number { |
| 7 | + return x * x; |
| 8 | +} |
| 9 | + |
| 10 | +const numbers: number[] = [1, 2, 3, 4]; |
| 11 | +const squares: number[] = processArray(square, numbers); |
| 12 | +console.log(squares); |
| 13 | + |
| 14 | +// Function that returns another function |
| 15 | +function createSum(x: number): (y: number) => number { |
| 16 | + return function(y: number): number { |
| 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: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 26 | +const evenNumbers: number[] = numbers10.filter(num => num % 2 === 0); |
| 27 | +const evenSquares: number[] = evenNumbers.map(num => num * num); |
| 28 | +const sumEvenSquares: number = 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 | +interface Student { |
| 36 | + name: string; |
| 37 | + birthdate: Date; |
| 38 | + qualifications: number[]; |
| 39 | +} |
| 40 | + |
| 41 | +const students: Student[] = [{ |
| 42 | + name: 'Isaac', |
| 43 | + birthdate: new Date(2001, 9, 21), |
| 44 | + qualifications: [9.0, 10, 9.5, 9.8] |
| 45 | +}, |
| 46 | +{ |
| 47 | + name: 'Juan', |
| 48 | + birthdate: new Date(2001, 6, 22), |
| 49 | + qualifications: [9, 8, 8.5, 9.5] |
| 50 | +}, |
| 51 | +{ |
| 52 | + name: 'Luis', |
| 53 | + birthdate: new Date(2003, 10, 5), |
| 54 | + qualifications: [6, 7, 7.5, 8] |
| 55 | +}, |
| 56 | +{ |
| 57 | + name: 'María', |
| 58 | + birthdate: new Date(2000, 4, 20), |
| 59 | + qualifications: [10, 9.5, 10, 10] |
| 60 | +}]; |
| 61 | + |
| 62 | +const studentAverage = students.map(student => { |
| 63 | + const average = student.qualifications.reduce((sum, score) => sum + score, 0) / student.qualifications.length; |
| 64 | + return { name: student.name, average: parseFloat(average.toFixed(2)) }; |
| 65 | +}); |
| 66 | +console.log(studentAverage); |
| 67 | + |
| 68 | +const bestStudents = studentAverage.filter(student => student.average >= 9); |
| 69 | +console.log(bestStudents); |
| 70 | + |
| 71 | +const datesOfBirthInOrder = students.sort((a, b) => b.birthdate.getTime() - a.birthdate.getTime()); |
| 72 | +datesOfBirthInOrder.forEach(student => { |
| 73 | + const date = student.birthdate; |
| 74 | + console.log(`\n${student.name}: ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`); |
| 75 | +}); |
| 76 | + |
| 77 | +const allQualifications = students.flatMap(student => student.qualifications); |
| 78 | +const highestScore = Math.max(...allQualifications); |
| 79 | + |
| 80 | +console.log(`\nThe highest score is ${highestScore}`); |
0 commit comments