-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (27 loc) · 923 Bytes
/
index.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
/**
* @param {number} distance
* @param {{name: string, consumption: number}[]} sleighs
* @returns {string|null}
*/
function selectSleigh(distance, sleighs) {
const firstInvalidSleighIndex = sleighs.findIndex(
sleight => sleight.consumption * distance > 20
)
if (firstInvalidSleighIndex === 0) return null
if (firstInvalidSleighIndex === -1) return sleighs.pop().name
return sleighs[firstInvalidSleighIndex - 1].name
}
/**
* @param {number} distance
* @param {{name: string, consumption: number}[]} sleighs
* @returns {string|null}
*/
function selectSleighOptimized(distance, sleighs) {
sleighs.unshift({name: null, consumption: 0})
sleighs.push({name: 'Too much consumption', consumption: 21})
const invalidSleighIndex = sleighs.findIndex(
sleight => sleight.consumption * distance > 20
)
return sleighs[invalidSleighIndex - 1].name
}
export {selectSleigh, selectSleighOptimized}