Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
Created May 10, 2022 15:55
Show Gist options
  • Save fazeelanizam13/ed4c59725dcce5538831af442ad8b0b1 to your computer and use it in GitHub Desktop.
Save fazeelanizam13/ed4c59725dcce5538831af442ad8b0b1 to your computer and use it in GitHub Desktop.
// validate input array elements
function isIntegerArray (input) {
// if an array
if (Array.isArray(input)) {
// if empty array
if (input.length < 1) return false
// iterate through each element and return false if one of them happens to be not an integer
for (let i = 0; i < input.length; i++) {
if (Number.isInteger(input[i])) continue
else return false
}
return true
// if not an array
} else return false
}
function findMissingNumber (input) {
if (isIntegerArray(input)) {
// iterate through all numbers
for (let i = 0; i < input.length; i++) {
// check difference between next number and current number
const difference = input[i+1] - input[i]
// if greater than 1, current number + 1 is missing number
if (difference > 1) return input[i] + 1
}
} else return 'Please input an array of integers.'
}
console.log(findMissingNumber([1, 2, 3, 4, 5, 6, 7, 9, 10]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment