我正在研究代码战争问题,我的任务是将全零移动到数组的末尾,而不移动所述数组中任何其他元素的位置。我遍历数组,确定是否找到0,然后将其拼接到数组之外,同时将0添加到第二个数组。在函数末尾,我将两个数组合并并返回。
var moveZeros = function (arr) {
// TODO: Program me
var Zeros = [];
for (var i = 0; i < arr.length; i++) {
if(arr[i] === 0){
arr.splice(i, 1);
Zeros.push(0);
}
}
return arr.concat(Zeros);
//loop through arr
//if arr[i] is 0
//take out of arr and push into newarr
//return arr + newarr
}
对于测试案例,我知道我通过了什么输入。但是我没有通过三个测试用例:
Expected: '[9,9,1,2,1,1,3,1,9,9,0,0,0,0,0,0,0,0,0,0]', instead got: '[9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]'
Expected: '["a","b","c","d",1,1,3,1,9,9,0,0,0,0,0,0,0,0,0,0]', instead got: '["a",0,"b","c","d",1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]'
Expected: '["a","b",null,"c","d",1,false,1,3,[],1,9,{},9,0,0,0,0,0,0,0,0,0,0]', instead got: '["a",0,"b",null,"c","d",1,false,1,3,[],1,9,0,{},0,9,0,0,0,0,0,0,0]'
如您所见,它们显示了预期的输出,但没有显示输入,因此我无法调试问题。有人可以给我一些有关我哪里出问题/我想念什么的见解吗?