Skip to content

Instantly share code, notes, and snippets.

@anushshukla
Last active November 12, 2020 21:16
Show Gist options
  • Select an option

  • Save anushshukla/54920622c4b4235c789f4b8452b23a15 to your computer and use it in GitHub Desktop.

Select an option

Save anushshukla/54920622c4b4235c789f4b8452b23a15 to your computer and use it in GitHub Desktop.
Find Highest Number in an Array of Numbers
const numbersList = [3, 6, 2, 99, 32, 5, 89, 32];
Array.prototype.findHighestNumber = function(currentIndex = 1, lastHighestNumber = this[0]) {
const currentNumber = this[currentIndex];
lastHighestNumber = lastHighestNumber > currentNumber ? lastHighestNumber : currentNumber;
if (currentIndex === this.length - 1) {
return lastHighestNumber;
}
const nextIndex = currentIndex + 1;
return this.findHighestNumber(nextIndex, lastHighestNumber);
}
console.log(`Highest number in "${numbersList}" is ${numbersList.findHighestNumber()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment