-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWho has the most money.js
79 lines (59 loc) · 1.95 KB
/
Who has the most money.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
// You're going on a trip with some students and it's up to you to keep track of how much money each Student has.
// A student is defined like this:
//
// class Student {
// constructor(name, fives, tens, twenties) {
// this.name = name;
// this.fives = fives;
// this.tens = tens;
// this.twenties = twenties;
// }
// }
// As you can tell, each Student has some fives, tens, and twenties.
// Your job is to return the name of the student with the most money.
// If every student has the same amount, then return "all".
//
// Notes:
//
// Each student will have a unique name
// There will always be a clear winner: either one person has the most, or everyone has the same amount
// If there is only one student, then that student has the most money
//==============mock=============================
class Student {
constructor(name, fives, tens, twenties) {
this.name = name;
this.fives = fives;
this.tens = tens;
this.twenties = twenties;
}
}
const andy = new Student("Andy", 0, 0, 2);
const stephen = new Student("Stephen", 0, 4, 0);
const eric = new Student("Eric", 8, 1, 0);
//==============mock=============================
function mostMoney(students) {
if (students.length === 1) return students[0].name
function sumMoney(student) {
return (student.fives * 5) + (student.tens * 10) + (student.twenties * 20)
}
const arrMoney = students.map(s => sumMoney(s))
let largest = {index: 0, num: arrMoney[0]}
for (let i = 1; i <= arrMoney.length; i++) {
if (arrMoney[i] > largest.num) {
largest.num = arrMoney[i]
largest.index = i
}
}
let count = 0;
for (let i = 0; i < arrMoney.length; i++) {
if (arrMoney[i] === largest.num) {
count++
}
}
if (count > 1) {
return 'all'
} else {
return students[largest.index].name
}
}
mostMoney([andy, stephen, eric])