返回 Null - 为什么会出错?
Returning Null - why is this going wrong?
我使用此代码返回 null,但是我看不到 why/where 这是错误的。 (这可能是由于排序功能,因为我是从一个关于如何对数组排序的好评片段中得到的)。比起正确答案,我更在乎理解。
我要完成的任务:
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
Example
For statues = [6, 2, 3, 8]
, the output should be
solution(statues) = 3
.
Ratiorg needs statues of sizes 4, 5 and 7.
我的代码与思考:
function solution(statues) {
let total = 0;
statues.sort(function(a, b) { //From what I understand this should sort the array numerically
return a - b;
});
for (let i = 1; i < statues.length; i++) { //iterate through the array comparing the index to the one before it
if (statues[i + 1] != (statues[i] + 1)) { //if there is a diff between index of more than 1 it will add the diff
total += statues[i + 1] - statues[i]; //to the total variable
}
}
return total;
}
const result = solution([6, 2, 3, 8]);
console.log(result);
感谢发表评论的人帮助我发现我的逻辑中存在一些错误 - 我现在更改了 if 语句以比较从 1 开始的索引和它后面的索引,即索引 1 与索引 0 的比较。在最重要的是,以下行 total += statues[i] - statues[i-1];
存在问题
我用 for 循环替换了它,这样它就可以计数而不是简单地减去它。
function solution(statues) {
//[6, 2, 3, 8] --- 2,3,6,8 --- 3,6 7-2,
let total = 0;
statues.sort(function(a, b) {
return a - b;
});
for(let i =1; i<statues.length; i++){
if(statues[i] != (statues[i-1]+1) ){
//total += statues[i] - statues[i-1];
for(let j = statues[i-1]; j<statues[i]-1; j++){
total += 1;
}
}
}
return total;
}
我使用此代码返回 null,但是我看不到 why/where 这是错误的。 (这可能是由于排序功能,因为我是从一个关于如何对数组排序的好评片段中得到的)。比起正确答案,我更在乎理解。
我要完成的任务:
Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.
Example
For
statues = [6, 2, 3, 8]
, the output should besolution(statues) = 3
.Ratiorg needs statues of sizes 4, 5 and 7.
我的代码与思考:
function solution(statues) {
let total = 0;
statues.sort(function(a, b) { //From what I understand this should sort the array numerically
return a - b;
});
for (let i = 1; i < statues.length; i++) { //iterate through the array comparing the index to the one before it
if (statues[i + 1] != (statues[i] + 1)) { //if there is a diff between index of more than 1 it will add the diff
total += statues[i + 1] - statues[i]; //to the total variable
}
}
return total;
}
const result = solution([6, 2, 3, 8]);
console.log(result);
感谢发表评论的人帮助我发现我的逻辑中存在一些错误 - 我现在更改了 if 语句以比较从 1 开始的索引和它后面的索引,即索引 1 与索引 0 的比较。在最重要的是,以下行 total += statues[i] - statues[i-1];
存在问题
我用 for 循环替换了它,这样它就可以计数而不是简单地减去它。
function solution(statues) {
//[6, 2, 3, 8] --- 2,3,6,8 --- 3,6 7-2,
let total = 0;
statues.sort(function(a, b) {
return a - b;
});
for(let i =1; i<statues.length; i++){
if(statues[i] != (statues[i-1]+1) ){
//total += statues[i] - statues[i-1];
for(let j = statues[i-1]; j<statues[i]-1; j++){
total += 1;
}
}
}
return total;
}