Last active
December 3, 2022 15:38
-
-
Save MohdSaifulM/8cc95c9e7c4c43b837222c378e75b068 to your computer and use it in GitHub Desktop.
Algorithms - Recursion
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
function capitalizeFirst (arr) { | |
// Base case | |
if (arr.length === 1) return arr[0][0].toUpperCase() + arr[0].slice(1); | |
// Recursive case - return capitalize first elem of array concat with sliced arr | |
return [arr[0][0].toUpperCase() + arr[0].slice(1)].concat(capitalizeFirst(arr.slice(1))) | |
} | |
// capitalizeFirst(['car','taco','banana']); // ['Car','Taco','Banana'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment