# JavaScript Challenge - Find the Missing Numbers

Sneaky. It would be interesting to know how expensive this approach is. Clever, all the same. Now the question is, which is quicker, `.has()` or `.indexOf()`? Don’t ask me, we’ll need to find this out.

The Big-O question, that also remains on the table.

1 Like

this is my code for this challenge!

``````function missingNos(array, k) {
let missingNumber = [];
let count = k;
for (let i = 1; i< array[array.length-1]; i++){
if (array.indexOf(i) === -1) {
missingNumber.push(i);
count--;
};
if (count === 0){
break;
}
};
return missingNumber;
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;
``````

Not the best approach i’m sure but I’ve completed this using sets.

``````        function missingNos(array, k) {

const lastNum = array[array.length - 1];

const nSet = new Set();

for(i = 1; i <= lastNum; i++){
}

const missing = [];
array.forEach((value, index) => {

if(nSet.has(value)){
nSet.delete(value);

}})

nSet.forEach((value, index) => {
missing.push(value);

})

return missing.sort();
}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];

console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;``````

Would like to actually use ‘k’, but first solution that came to mind here.

``````function missingNos(array, k) {
const minNum = Math.min(...array);
const maxNum = Math.max(...array);
let mmArray = [];
let finalArray = [];
for (let i = minNum; i < maxNum + 1; i++) {
mmArray.push(i);
}
for (let i = 0; i < mmArray.length; i++) {
if (!array.includes(mmArray[i])) {
finalArray.push(mmArray[i]);
}
}
return finalArray;
}

const testArray = [4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;`````````
``````function missingNos(array, k) {

const fn=array[0];
const ln=array[array.length-1];
let missingNumbers=[];
for(let i=fn; i < ln; i++){
if(!array.includes(i)){
missingNumbers.push(i)
}
}

return missingNumbers

}

const testArray = [1, 2, 4, 5, 6, 7, 8, 10];
console.log(missingNos(testArray, 2));

// Leave this so we can test your code:
module.exports = missingNos;
``````
``````const _ = require('lodash');

function missingNos(array, k) {
return _.range(_.first(array), _.last(array))
.reduce((res, _, index) => {
if (array.indexOf(index + 1) < 0) {
res.push(index + 1);
}
return res
}, []);
}
// Leave this so we can test your code:
module.exports = missingNos;
``````

My solution passed all the tests - but something seemed off about it…
My first suspicion something was wrong was at no point did I include k in my function. Later it occurred to me, none of the tests take into account if the missing tiles fall at the beginning or the end of the array. My solution passed, but it shouldn’t have, because it only works if the missing tiles are between 1 and the highest number provided in the array:

``````function missingNos(array, k) {
let arr = array.sort((a,b) => a - b);
let solution = [];
arr.reduce((prev,curr) => {
if ((curr - prev) > 1) {
let i = 1;
while (i < (curr - prev)) {
solution.push(prev + i);
i++;
}
};
return curr;
})
return solution;
}
``````

I’m going to try to rework it, but unless I missed something specified in the instructions, it looks like the problem’s testing could use some adjustments.

I corrected it to check for missing tiles at the beginning and end of the array, but it’s not as elegant as I would have liked and I’d welcome any alternatives. Array.reduce is still challenging for me, I was able to apply it to the original step of my function, location missing tiles in the middle, but I feel like if I had a better grasp of it, I should be able to use it on the rest of the problem…

``````function missingNos(array, k) {
let arr = array.sort((a,b) => a - b);
let solution = [];
if (arr[0] > 1) {
let i = 1;
while (i < arr[0]) {
solution.push(i);
i++
}
}
arr.reduce((prev,curr) => {
if ((curr - prev) > 1) {
let i = 1;
while (i < (curr - prev)) {
solution.push(prev + i);
i++;
}
};
return curr;
})
let i = 1;
while (solution.length < k) {
let lastTile = arr[arr.length -1];
solution.push(lastTile + i);
i++;
}
return solution;
}
``````