diff options
Diffstat (limited to 'challenge-080/dave-jacoby/node/ch-2.js')
| -rwxr-xr-x | challenge-080/dave-jacoby/node/ch-2.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-080/dave-jacoby/node/ch-2.js b/challenge-080/dave-jacoby/node/ch-2.js new file mode 100755 index 0000000000..a47fe0eb2b --- /dev/null +++ b/challenge-080/dave-jacoby/node/ch-2.js @@ -0,0 +1,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; +} + + |
