Last active
November 12, 2020 21:16
-
-
Save anushshukla/54920622c4b4235c789f4b8452b23a15 to your computer and use it in GitHub Desktop.
Find Highest Number in an Array of Numbers
This file contains hidden or 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
| 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