学习不会让人秃头 2022-09-08 21:30 采纳率: 50%
浏览 256
已结题

js数组添加有长度但是没有数据

js 有长度但是没有数据,考虑到是可能是异步问题或者深拷贝,但是不知道怎么修改
    let candidates = [2, 3, 6, 7]
    let target = 7
    let path = []
    let sum = 0
    let startindex = 0
    var res = []
    function backTracing(candidates, target, sum, startindex) {
        if (sum > target) {
            return
        }
        if (sum === target) {
            res.push(path)
            console.log(path);
            return res
        }

        for (let i = startindex; i < candidates.length; i++) {
            path.push(candidates[i])
            sum += candidates[i]
            // console.log(sum);
            backTracing(candidates, target, sum, i)
            sum -= candidates[i]
            path.pop()
        }

        return res
    }


    abc = backTracing(candidates, target, sum, startindex)

        console.log(abc);let candidates = [2, 3, 6, 7]
    let target = 7
    let path = []
    let sum = 0
    let startindex = 0
    var res = []
    function backTracing(candidates, target, sum, startindex) {
        if (sum > target) {
            return
        }
        if (sum === target) {
            res.push(path)
            console.log(path);
            return res
        }

        for (let i = startindex; i < candidates.length; i++) {
            path.push(candidates[i])
            sum += candidates[i]
            // console.log(sum);
            backTracing(candidates, target, sum, i)
            sum -= candidates[i]
            path.pop()
        }

        return res
    }


    abc = backTracing(candidates, target, sum, startindex)

        console.log(abc);
运行结果及报错内容

img

我想要达到的结果

img

  • 写回答

1条回答 默认 最新

  • 音药 2022-09-09 09:03
    关注

    res.push(path) 改成 res.push([...path])

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月21日
  • 已采纳回答 9月13日
  • 创建了问题 9月8日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘