为了账号安全,请及时绑定邮箱和手机立即绑定

JS变量被清空

JS变量被清空

智慧大石 2018-11-15 18:17:10
代码中的变量莫名奇妙的被清空,如下图所示:代码如下:function rolldiceSumProb(arr, sides){    let prob, result=[];           let dig = function(target, count, methods) {        if (count > sides) return false        console.log('dig', target, count)        for (let i=1; i<=6; i++) {            console.log('target:', target, 'count:', count, 'cur_i:', i, target+i==arr, sides==count)            if (target+i==arr && sides==count) {                methods.push(i)                result.push(methods)                console.log(methods, result, 'quit')                methods.pop()                return false            }            else {                methods.push(i)                if (target+i < arr) dig(target+i, count+1, methods)                methods.pop()            }        }    }    dig(0, 1, [])    console.log('res', result)    return prob;}rolldiceSumProb(11, 2)
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

methods 一直都是用的同一个……虽然它被添加到 result 里了,但是只是添加的引用,并不是复制了一个的, 以你可以添加个复制的结果,比如

result.push([...methods]);

或者用 es5 语法

result.push([].concat(methods));


查看完整回答
反对 回复 2018-12-18
  • 1 回答
  • 0 关注
  • 441 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号