我是用js写的kolakoski那题,但通过率只有20%,有人帮我看看哪里有错吗?

let temp = readline().split(' ');
let n = temp[0], m = temp[1];   // n为上限个数,m为构成序列的数字的个数
let arr = readline().split(' ');    // arr为构成序列的数字
let count = 0;  // 总数
while(count <= n){
    let temp = Number(arr[Math.floor(Math.random() * Number(m))]);      // 从arr中随机选一个数字temp
    if((count + temp) < n){     // 总数加上temp个数字不超上限个数
        count += temp;
        for(let i = 0; i < temp; i++){
            console.log(temp);
        }
    }
    else {  // 总数加上temp个数字超上限个数,打印(上限个数减当前总数)个temp
        for(let i = 0; i < (n - count); i++){
            console.log(temp);
        }
        break;
    }
}