missingNumber()
This weeks coding challange is to find the missing number in a string from 1 to 100. I came to a very quick and simple solution to this one. If we know there is only one number missing in the array all we have to do is make sure the next number in the array is present. When using a for loop each individual number would be arr[i] and the proceeding number will be arr[i + 1]. If arr[i + 1] does not equal arr[i] + 1 that would mean our missing number is arr[i] + 1 !.
fuction missingNumber(arr){
for(let i = 0; i < arr.length; i++){
if (arr[i + 1] != arr[i] = 1){
return arr[i] + 1
}
}
}
Thats all there is to it! The problem would be a little more complicated if the array was not given to us in incrementing order, but all we would really have to do in that case would be to sort the array and use the same for loop in our sorted array.
It feels good to come up with such a simple solution every now and then. Who said coding had to be hard!