-
Notifications
You must be signed in to change notification settings - Fork 0
/
p0-w4-e15-highestScore.js
117 lines (105 loc) · 2.32 KB
/
p0-w4-e15-highestScore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Week-4 Exercise 15 Highest Score
/*
Implementasikan function highestScore untuk mendapatkan student dengan nilai tertinggi dari setiap class:
Output yang diharapkan berupa Object Literal dengan format sebagai berikut:
{
<class>: { name: <name>, score: <score> },
<class>: { name: <name>, score: <score> },
<class>: { name: <name>, score: <score> }
}
*/
//Object Literal
function highestScore(students) {
var classH = [];
//var objHigh = {};
var objClass = {};
//looping simpan group class
for(var i=0; i<students.length; i++) {
var duplikat = false;
for(j=0; j<classH.length; j++) {
if(classH[j]===students[i]['class']) {
duplikat = true;
}
}
if(duplikat===false) {
classH.push(students[i]['class']);
}
}
//console.log(classH);
for(i=0; i<classH.length; i++) {
var scoreH = 0;
//console.log(classH[i]);
for(var j=0; j<students.length; j++) {
if(classH[i]===students[j]['class'] && students[j]['score']>scoreH) {
scoreH = students[j]['score'];
var objHigh = {};
objHigh['name']=students[j]['name'];
objHigh['score']=scoreH;
//console.log(objHigh);
//var objClass = {};
objClass[classH[i]] = objHigh;
}
}
}
//console.log(objClass);
return objClass;
}
console.log(highestScore([
{
name: 'Dimitri',
score: 90,
class: 'foxes'
},
{
name: 'Alexei',
score: 85,
class: 'wolves'
},
{
name: 'Sergei',
score: 74,
class: 'foxes'
},
{
name: 'Anastasia',
score: 78,
class: 'wolves'
}
]));
// {
// foxes: { name: 'Dimitri', score: 90 },
// wolves: { name: 'Alexei', score: 85 }
// }
console.log(highestScore([
{
name: 'Alexander',
score: 100,
class: 'foxes'
},
{
name: 'Alisa',
score: 76,
class: 'wolves'
},
{
name: 'Vladimir',
score: 92,
class: 'foxes'
},
{
name: 'Albert',
score: 71,
class: 'wolves'
},
{
name: 'Viktor',
score: 80,
class: 'tigers'
}
]));
// {
// foxes: { name: 'Alexander', score: 100 },
// wolves: { name: 'Alisa', score: 76 },
// tigers: { name: 'Viktor', score: 80 }
// }
console.log(highestScore([])); //{}