Created
May 10, 2022 15:55
-
-
Save fazeelanizam13/fc37901c48309173d6bce34aadf0aecd 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 findMaxAndMin (input) { | |
// assign first array element to both as initial values | |
let max = input[0] | |
let min = input[0] | |
if (isIntegerArray(input)) { | |
// iterate through array starting from second element | |
for (let i = 1; i < input.length; i++) { | |
// if current element greater than current max, assign it to max | |
if (input[i] > max) max = input[i] | |
// if current element lesser than current min, assign it to min | |
if (input[i] < min) min = input[i] | |
} | |
return { max, min } | |
} else return 'Please input an array of integers.' | |
} | |
console.log(findMaxAndMin([1, 68, 3, -488, 5, 168, 9, 1, 1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment