Created
May 10, 2022 15:55
-
-
Save fazeelanizam13/ed4c59725dcce5538831af442ad8b0b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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