Skip to content

Instantly share code, notes, and snippets.

@MohdSaifulM
Last active December 3, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohdSaifulM/8cc95c9e7c4c43b837222c378e75b068 to your computer and use it in GitHub Desktop.
Save MohdSaifulM/8cc95c9e7c4c43b837222c378e75b068 to your computer and use it in GitHub Desktop.
Algorithms - Recursion
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