-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproblem54.js
73 lines (53 loc) · 1.45 KB
/
problem54.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
/*
Create a function called highestRunScorer that will
Receive a 2d array called playersInfo
Based on highest score, return the name of the player
Things required
Variable
If-else
Loop
Function
Array
*/
/* =========================================
Method One: (Complex)
============================================= */
/* const highestRunScorer = players => {
const allPlayersRun = [];
for( let i = 0; i < players.length; i++){
const player = players[i];
const playerRun = player[1];
allPlayersRun.push(playerRun);
}
const topRuns = Math.max(...allPlayersRun);
for( let x = 0; x < players.length; x++){
if(players[x][1] == topRuns){
return players[x][0];
}
}
}
*/
/* =========================================
Method Two: (Simple)
============================================= */
function highestRunScorer(players){
let highestScorer = players [0][0];
let highestRun = players[0][1];
for(var i = 1; i < players.length; i++){
if(highestRun < players[i][1]){
highestRun = players[i][1]
highestScorer = players[i][0];
}
}
return highestScorer;
}
const playersInfo = [
["Sakib", 19],
["Musfique", 35],
["Asraful", 95],
["Mahmudullah", 45],
["Mashrafee", 13],
["Shourav", 115]
];
const result = highestRunScorer(playersInfo);
console.log(`The highest run scorer is ${result}`);