尝试在 VS Code 中使用 slice() 复制数组时出现奇怪的结果
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论