学习不会让人秃头 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日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改