So I have a function that finds the missing number given upper and lower bounds and the function logic works fine as
const findMissingNumber = (arr, upperBound, lowerBound) => {
arr = arr.sort((a, b) => a - b);
let correctArray = [lowerBound];
for (let i = 1; i < upperBound; i++, lowerBound++) {
if (lowerBound + 1 < upperBound)
correctArray[i] = lowerBound + 1;
else
break
}
correctArray.push(upperBound);
arr.forEach((o, i, a) => {
if (a[i] !== correctArray[i])
return console.log(correctArray[i])
})
}
this does log to console the value of 8,but I if use return correctArray[i]
with console.log(findMissingNumber(arrayOfIntegers, 9, 3))
i get undefined
, what is the cause of this?