Created
March 29, 2019 20:36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Solution for: http://codesnakers.com/projects/1.png | |
*/ | |
function solution(ranks) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
var len = ranks.length; | |
if(len===undefined){ | |
return 0; | |
} | |
var keyArr = []; | |
var temp; | |
var soldierCount = 0; | |
for(var i=0; i<len; i++){ | |
temp = keyArr[ranks[i]]; | |
keyArr[ranks[i]] = temp===undefined?1:(temp+1); | |
} | |
for(var i=0; i<keyArr.length-1; i++){ | |
temp = keyArr[i]; | |
if(temp===undefined){ | |
continue; | |
} | |
if(temp!==undefined && keyArr[i+1]!==undefined){ | |
soldierCount = soldierCount + temp; | |
} | |
} | |
console.log(keyArr, soldierCount); | |
return soldierCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment