Permutation generation in javascript