aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/dave-jacoby/node/ch-2.js
blob: a47fe0eb2b2b6de5a5679e7a7ed9ee5aa6eb7174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"use strict"

console.log( candy_count( [1, 2, 2] ) );
console.log( candy_count( [1, 4, 3, 2] ) );


function candy_count( candidates ) {
    let total = 0;
    console.log( candidates );
    for ( let i in candidates ) {
        i = parseInt(i); // string by default
        let v = candidates[i];
        let prev = candidates[i-1] || 0;
        let next = candidates[i+1] || 0;
        total ++;
        if ( v > prev && prev != 0 ) { total ++ }
        if ( v > next && next != 0 ) { total ++ }
    }
    return total;
}