I am beginning to learn Typescript on Codesignal platform. I came across this exercise
Here is the question for those who cannot access the link:
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
makeArrayConsecutive2(statues) = 3
. Ratiorg needs statues of sizes 4, 5 and 7.
My solution was this
function makeArrayConsecutive2(statues: number[]): number {
let sorted: number[] = statues.sort();
let full = [];
for (let i = sorted[0]; i <= sorted[sorted.length-1]; i++) {
full.push(i);
}
var diff = full.filter(myCallBack);
function myCallBack(el) {
return sorted.indexOf(el) < 0;
}
return diff.length;
When I ran the tests, only 9/10 of them were passed. The one I failed was a hidden test.
Also, when I try not to use the filter() method and instead calculate the difference between the sorted and full array by:
return full.length - sorted.length;
I got only 8/10.
Could anyone give me the reason(s)?