-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevels.js
More file actions
52 lines (41 loc) · 1.12 KB
/
levels.js
File metadata and controls
52 lines (41 loc) · 1.12 KB
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
'use strict';
/*
* Complete the 'maximumPoints' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY costs
*/
//3,1,7
let costs=[5,2,3,1,4,5,3];
//console.log(maximumPoints(10,costs));
function maximumPoints(k, costs) {
// Write your code here
//let indice_max=0;
let valor_max=0;
let puntos=0;
let gastos=0;
let salto_nivel=false;
for(let i=3; i<costs.length;i++ ){
if(costs[i]>valor_max){
valor_max=costs[i];
//indice_max=i;
}
if(gastos + costs[i] <= k){
gastos += costs[i]; // 10
puntos++; //4
}else{
if(salto_nivel){
break;
}else{
//5
gastos -= valor_max;
puntos--; //2
salto_nivel=true; //true
i--;//i 2
}
}
}
return puntos;
}