Permutation Game Hackerrank Javascript