@@ -14,34 +14,21 @@ Array.prototype.customMap = function (callback) {
14
14
15
15
console . log ( arr . customMap ( ( n ) => n * 2 ) ) ;
16
16
17
- /*
18
- * DIFICULTAD EXTRA (opcional):
19
- * Dada una lista de estudiantes (con sus nombres, fecha de nacimiento y
20
- * lista de calificaciones), utiliza funciones de orden superior para
21
- * realizar las siguientes operaciones de procesamiento y análisis:
22
- * - Promedio calificaciones: Obtiene una lista de estudiantes por nombre
23
- * y promedio de sus calificaciones.
24
- * - Mejores estudiantes: Obtiene una lista con el nombre de los estudiantes
25
- * que tienen calificaciones con un 9 o más de promedio.
26
- * - Nacimiento: Obtiene una lista de estudiantes ordenada desde el más joven.
27
- * - Mayor calificación: Obtiene la calificación más alta de entre todas las
28
- * de los alumnos.
29
- * - Una calificación debe estar comprendida entre 0 y 10 (admite decimales).
30
- */
31
-
17
+ //EXTRA
32
18
let myStudents = [ ] ;
33
19
let bestStudents = [ ] ;
34
20
35
21
class Student {
36
22
constructor ( name , grades , birthDate ) {
37
23
this . name = name ;
38
- this . grades = grades ;
39
24
this . birthDate = birthDate ;
25
+ this . grades = grades ;
40
26
this . age = this . getAge ( ) ;
41
27
this . average = this . getAverage ( ) ;
28
+
42
29
myStudents . push ( this ) ;
43
30
44
- if ( this . getAverage ( ) >= 9 ) {
31
+ if ( this . average >= 9 ) {
45
32
bestStudents . push ( this ) ;
46
33
}
47
34
}
@@ -57,44 +44,72 @@ class Student {
57
44
58
45
getAge ( ) {
59
46
let now = new Date ( ) ;
60
- let age = Math . floor (
61
- ( now . getTime ( ) - this . birthDate . getTime ( ) ) / ( 365 * 24 * 60 * 60 * 1000 )
62
- ) ;
47
+ let age =
48
+ ( now . getTime ( ) - this . birthDate . getTime ( ) ) / ( 365 * 24 * 60 * 60 * 1000 ) ;
63
49
return age ;
64
50
}
65
51
}
66
52
67
53
new Student ( 'Sophie' , [ 8 , 8 , 7 , 9 ] , new Date ( 2002 , 8 , 21 ) ) ;
54
+ new Student ( 'Rebeca' , [ 8 , 9 , 10 , 9 ] , new Date ( 2001 , 4 , 16 ) ) ;
68
55
new Student ( 'David' , [ 7 , 6 , 9 , 8 ] , new Date ( 2003 , 0 , 15 ) ) ;
69
56
new Student ( 'Eliezer' , [ 4 , 6 , 7 , 5 ] , new Date ( 2005 , 1 , 21 ) ) ;
70
- new Student ( 'Rebeca' , [ 8 , 9 , 10 , 9 ] , new Date ( 2001 , 4 , 16 ) ) ;
71
57
new Student ( 'Robert' , [ 10 , 10 , 10 , 9 ] , new Date ( 2002 , 0 , 16 ) ) ;
72
58
73
59
function getHighestAverage ( ) {
74
60
let result ;
75
61
let average = 0 ;
76
62
bestStudents . forEach ( ( element ) => {
77
- if ( element . getAverage ( ) > average ) {
63
+ if ( element . average > average ) {
78
64
result = element ;
79
65
}
80
66
} ) ;
81
67
return result ;
82
68
}
83
-
84
69
let highestAverage = getHighestAverage ( ) ;
85
70
86
- function sortByAge ( ) { }
71
+ //Pendiente por revisar
72
+ function sortByAge ( ) {
73
+ for ( let i = 0 ; i < myStudents . length ; i ++ ) {
74
+ for ( let j = i + 1 ; j < myStudents . length ; j ++ ) {
75
+ if ( myStudents [ i ] . age > myStudents [ j ] . age ) {
76
+ let change = myStudents [ i ] ;
77
+ myStudents [ i ] = myStudents [ j ] ;
78
+ myStudents [ j ] = change ;
79
+ }
80
+ }
81
+ }
87
82
88
- console . log (
89
- `\n${ highestAverage . name } ha obtenido el mayor promedio de la clase: ${ highestAverage . average } \n`
90
- ) ;
83
+ return myStudents ;
84
+ }
91
85
92
- console . log ( myStudents . find ( ( element ) => element . name === 'David' ) ) ;
86
+ let sortedByAge = sortByAge ( ) ;
93
87
94
- myStudents . customMap ( ( element ) => {
95
- console . log ( `\nEl promedio de ${ element . name } es de ${ element . average } ` ) ;
88
+ let sortedByAverage = myStudents . sort ( function ( a , b ) {
89
+ return a . average - b . average ;
96
90
} ) ;
97
91
98
- myStudents . customMap ( ( element ) => {
99
- console . log ( `\nEl nacimiento de ${ element . name } fue el ${ element . birthDate } ` ) ;
92
+ console . log ( '\nLISTA DE PROMEDIOS' ) ;
93
+
94
+ sortedByAverage . customMap ( ( element ) => {
95
+ console . log ( `\n${ element . name } : ${ element . average } ` ) ;
96
+ } ) ;
97
+
98
+ console . log ( '\nMEJORES ESTUDIANTES' ) ;
99
+
100
+ bestStudents . customMap ( ( element ) => {
101
+ console . log ( `\n${ element . name } : ${ element . average } ` ) ;
100
102
} ) ;
103
+
104
+ console . log ( '\nLISTA DE NACIMIENTOS' ) ;
105
+
106
+ sortedByAge . customMap ( ( element ) => {
107
+ console . log ( `\n${ element . name } : ${ element . birthDate . toLocaleString ( ) } ` ) ;
108
+ console . log ( `${ Math . floor ( element . age ) } años` ) ;
109
+ } ) ;
110
+
111
+ console . log ( '\nMEJOR PROMEDIO DE LA CLASE' ) ;
112
+
113
+ console . log (
114
+ `\nFelicidades a ${ highestAverage . name } . Con un ${ highestAverage . average } se ha coronado como el mejor promedio de la clase`
115
+ ) ;
0 commit comments