尝试在 VS Code 中使用 slice() 复制数组时出现奇怪的结果

发布于 2025-01-15 20:57:48 字数 1331 浏览 0 评论 0原文

我正在 freeCodeCamp 上进行“Chunky Monkey”练习,并尝试在 VS Code 上调试程序。但我得到的结果与我尝试在 fCC 控制台上运行它时得到的结果不同。 fCC 返回该情况的预期结果(原始数组),但我在 VS Code 上得到不同的输出。这是我编写的代码:

function chunkArrayInGroups(arr, size) {
    let groupArr = arr.slice(0);   // VS Code returns [4, 5, 6, 7]
    //console.log(groupArr)
    const groupTarget = Math.ceil(arr.length / size);
    console.log(groupTarget)
    let group = 1;
    console.log(group)
    let finalArr = [];
    for (let i = 0; i < size; i++) {  // iterates though the array 'size' times 
      if (group == groupTarget) {     // if for when we are on the last array to fill
        for (let j = 0; j < size; j++)  // for to iterate size amount of times to see if there is no undefined
        {
          if (groupArr[j] == undefined) { //if there is a undefined value, we'll make a smaller array for the last group 
            finalArr.push(groupArr.splice(0, j))
            console.log(finalArr)
          }
        }
      }
      finalArr.push(groupArr.splice(0, size))  // if we are not on the last group, we just splice the original value and push it 
      console.log(finalArr)
    } 
    return finalArr; // once loops end, we return the finalArray
  }
  
  chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 

希望有人能够提供帮助。我在 VS Code 上搞砸了什么吗?我只复制了我拥有的确切代码。

I'm doing the "Chunky Monkey" practice on freeCodeCamp and I'm trying to debug the program on VS Code. But I'm getting a different result than the one I get when I try to run it on the fCC console. fCC returns the expected result for the case, the original array, but I get a different output on VS Code. Here's the code I made:

function chunkArrayInGroups(arr, size) {
    let groupArr = arr.slice(0);   // VS Code returns [4, 5, 6, 7]
    //console.log(groupArr)
    const groupTarget = Math.ceil(arr.length / size);
    console.log(groupTarget)
    let group = 1;
    console.log(group)
    let finalArr = [];
    for (let i = 0; i < size; i++) {  // iterates though the array 'size' times 
      if (group == groupTarget) {     // if for when we are on the last array to fill
        for (let j = 0; j < size; j++)  // for to iterate size amount of times to see if there is no undefined
        {
          if (groupArr[j] == undefined) { //if there is a undefined value, we'll make a smaller array for the last group 
            finalArr.push(groupArr.splice(0, j))
            console.log(finalArr)
          }
        }
      }
      finalArr.push(groupArr.splice(0, size))  // if we are not on the last group, we just splice the original value and push it 
      console.log(finalArr)
    } 
    return finalArr; // once loops end, we return the finalArray
  }
  
  chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 

Hope someone is able to help. Did I mess up something on VS Code? I only copied the exact code that I had.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文