|
| 1 | +/* |
| 2 | + Higher order functions (HOF)... |
| 3 | +*/ |
| 4 | + |
| 5 | +console.log('Higher order functions (HOF)...') |
| 6 | + |
| 7 | +function toFilter<T>(array: T[], callback: (element: T) => boolean): T[] { |
| 8 | + const filteredArray: T[] = [] |
| 9 | + |
| 10 | + for (const element of array) { |
| 11 | + callback(element) && filteredArray.push(element) |
| 12 | + } |
| 13 | + |
| 14 | + return filteredArray |
| 15 | +} |
| 16 | + |
| 17 | +const numbers: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 18 | +console.log(`\nnumbers = [${numbers}]`) |
| 19 | + |
| 20 | +const oddNumbers: number[] = toFilter(numbers, (element) => element % 2 !== 0) |
| 21 | +console.log(`\noddNumbers = [${oddNumbers}]`) |
| 22 | + |
| 23 | +const evenNumbers: number[] = toFilter(numbers, (element) => element % 2 === 0) |
| 24 | +console.log(`\nevenNumbers = [${evenNumbers}]`) |
| 25 | + |
| 26 | +console.log( |
| 27 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 28 | +) |
| 29 | + |
| 30 | +/* |
| 31 | + Additional challenge... |
| 32 | +*/ |
| 33 | + |
| 34 | +console.log('Additional challenge...\n') |
| 35 | + |
| 36 | +interface Student { |
| 37 | + bornDate: Date |
| 38 | + name: string |
| 39 | + qualifications: number[] |
| 40 | +} |
| 41 | + |
| 42 | +function getAverageQualification(student: Student): number { |
| 43 | + const total: number = student.qualifications.reduce( |
| 44 | + (prevValue, currentValue) => prevValue + currentValue, |
| 45 | + 0 |
| 46 | + ) |
| 47 | + |
| 48 | + const averageQualification: number = total / student.qualifications.length |
| 49 | + |
| 50 | + return parseFloat(averageQualification.toFixed(2)) |
| 51 | +} |
| 52 | + |
| 53 | +const students: Student[] = [ |
| 54 | + { |
| 55 | + bornDate: new Date(2002, 20, 2), |
| 56 | + name: 'Lucas Hoz', |
| 57 | + qualifications: [7.5, 6.8, 8.2], |
| 58 | + }, |
| 59 | + { |
| 60 | + bornDate: new Date(2003, 10, 15), |
| 61 | + name: 'John Doe', |
| 62 | + qualifications: [9.1, 8.5, 7.9], |
| 63 | + }, |
| 64 | + { |
| 65 | + bornDate: new Date(2001, 5, 30), |
| 66 | + name: 'Jane Smith', |
| 67 | + qualifications: [8.7, 9.3, 10.0], |
| 68 | + }, |
| 69 | + { |
| 70 | + bornDate: new Date(2004, 2, 12), |
| 71 | + name: 'Alex Johnson', |
| 72 | + qualifications: [6.4, 7.2, 8.9], |
| 73 | + }, |
| 74 | + { |
| 75 | + bornDate: new Date(2000, 8, 5), |
| 76 | + name: 'Sarah Brown', |
| 77 | + qualifications: [9.5, 9.8, 9.2], |
| 78 | + }, |
| 79 | + { |
| 80 | + bornDate: new Date(2005, 11, 25), |
| 81 | + name: 'Michael Davis', |
| 82 | + qualifications: [7.1, 8.3, 7.7], |
| 83 | + }, |
| 84 | + { |
| 85 | + bornDate: new Date(2002, 1, 18), |
| 86 | + name: 'Emily Wilson', |
| 87 | + qualifications: [8.9, 9.6, 9.4], |
| 88 | + }, |
| 89 | + { |
| 90 | + bornDate: new Date(2003, 6, 9), |
| 91 | + name: 'Daniel Thompson', |
| 92 | + qualifications: [7.8, 8.1, 7.5], |
| 93 | + }, |
| 94 | +] |
| 95 | + |
| 96 | +console.log('Students...') |
| 97 | +console.table(students) |
| 98 | + |
| 99 | +const studentsWithNameAndAverageQualification = students.map((student) => ({ |
| 100 | + name: student.name, |
| 101 | + averageQualification: getAverageQualification(student), |
| 102 | +})) |
| 103 | + |
| 104 | +console.log('\nStudents with name and average qualification...') |
| 105 | +console.table(studentsWithNameAndAverageQualification) |
| 106 | + |
| 107 | +const studentsWithAverageQualificationMoreThanNine: Student[] = toFilter( |
| 108 | + students, |
| 109 | + (student) => { |
| 110 | + const qualificationAverage: number = getAverageQualification(student) |
| 111 | + return qualificationAverage >= 9 |
| 112 | + } |
| 113 | +) |
| 114 | + |
| 115 | +console.log('\nStudents with an average qualification more than nine...') |
| 116 | +console.table(studentsWithAverageQualificationMoreThanNine) |
| 117 | + |
| 118 | +const sortedStudentsByBornDate: Student[] = students.toSorted((a, b) => { |
| 119 | + return b.bornDate.getTime() - a.bornDate.getTime() |
| 120 | +}) |
| 121 | + |
| 122 | +console.log('\nSorted students by born date (youngest to oldest)...') |
| 123 | +console.table(sortedStudentsByBornDate) |
| 124 | + |
| 125 | +const studentsWithNameAndBestQualification = students.map((student) => ({ |
| 126 | + name: student.name, |
| 127 | + bestQualification: Math.max(...student.qualifications), |
| 128 | +})) |
| 129 | + |
| 130 | +console.log('\nStudents with name and best qualification...') |
| 131 | +console.table(studentsWithNameAndBestQualification) |
0 commit comments