带有1 ++参数的javascript递归

时间:2017-04-21 07:18:07

标签: javascript recursion

这是Dominoes游戏部分,我在递归函数中设置了三个参数,并重复它们以使玩家筹码成为对象。我想知道它有什么问题,只显示前两位球员的筹码T.T

这是代码:

function messUp() {
fichas=[];
for (i=0;i<7;i++) {
    for (e=0;e<=i;e++) {
        fichas.push('['+i+','+e+']');
    }
} 
fichas.sort(function(){return Math.random()-0.5});
document.write(fichas+'<br/><br/>');
}
messUp();

function players(name,hand) {
this.name=name;
this.hand=hand;
}

var main=prompt("Ingrese su nombre","");

function reAsign(h,f,l) {
if (h<4) {
    toHand=[];
    for (f;f<l;f++) {
        toHand.push(fichas[f]);
        delete fichas[f];
    }
    switch (h) {
        case 0: { 
            player1=new players(main,toHand);
            l=14;
        }   
        break;
        case 1: {
            player2=new players("Player 2",toHand);
            l=21;
        }   
        break;
        case 2: {
            player3=new players("Player 3",toHand);
            l=28;
        }
        break;
        case 3: {
            player4=new players("Player 4",toHand);
        }
        break;
    }
    reAsign(h+1,f+7,l+7);
}
}
reAsign(0,0,7);

document.write(player1.name+'<br/>');
document.write(player1.hand+'<br/>');
document.write(player2.name+'<br/>');
document.write(player2.hand+'<br/>');
document.write(player3.name+'<br/>');
document.write(player3.hand+'<br/>');
document.write(player4.name+'<br/>');
document.write(player4.hand+'<br/>');

1 个答案:

答案 0 :(得分:0)

原因是因为这一行

f

+7已经+7了,因为你迭代了它。当你7结束时,它会从14转到2,所以它正在跳过 - 所以3实际上是4的数字然后没有任何留给l的人。

reAsign(h+1,f,l); 相同。

应该是:

function messUp() {
fichas=[];
for (i=0;i<7;i++) {
    for (e=0;e<=i;e++) {
        fichas.push('['+i+','+e+']');
    }
} 
fichas.sort(function(){return Math.random()-0.5});
document.write(fichas+'<br/><br/>');
}
messUp();

function players(name,hand) {
this.name=name;
this.hand=hand;
}

var main=prompt("Ingrese su nombre","");

function reAsign(h,f,l) {
if (h<4) {
    toHand=[];
    for (f;f<l;f++) {
        toHand.push(fichas[f]);
        //delete fichas[f];
    }
    switch (h) {
        case 0: { 
            player1=new players(main,toHand);
            l=14;
        }   
        break;
        case 1: {
            player2=new players("Player 2",toHand);
            l=21;
        }   
        break;
        case 2: {
            player3=new players("Player 3",toHand);
            l=28;
        }
        break;
        case 3: {
            player4=new players("Player 4",toHand);
        }
        break;
    }
    reAsign(h+1,f,l);
}
}
reAsign(0,0,7);

document.write(player1.name+'<br/>');
document.write(player1.hand+'<br/>');
document.write(player2.name+'<br/>');
document.write(player2.hand+'<br/>');
document.write(player3.name+'<br/>');
document.write(player3.hand+'<br/>');
document.write(player4.name+'<br/>');
document.write(player4.hand+'<br/>');

const array = [{amount: 1000, cur: 'EUR'}, 
{amount: 1500, cur: 'GBD'}, 
{amount: 2000, cur: 'USD'}, 
{amount: 2500, cur: 'SSL'}];

const exclude = ['EUR', 'USD'];

var sum = array.map(x => (!exclude.includes(x.cur)) ? x.amount : false)
               .reduce((a, b) => a + b);

console.log(sum);