- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using a recursive function to capitalize each word in an array in JavaScript
We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things −
Make use of recursive approach
Make first word of each string element capital.
Our function should do this without using extra space for storing another array.
For example −
If the input array is −
const arr = ['apple', 'banana', 'orange', 'grapes'];
Then the array should be transformed to −
const output = ['Apple', 'Banana', 'Orange', 'Grapes'];
Example
The code for this will be −
const arr = ['apple', 'banana', 'orange', 'grapes']; const capitalize = (arr = [], ind = 0) => { const helper = (str = '') => { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }; if(ind < arr.length){ arr[ind] = helper(arr[ind]); return capitalize(arr, ind + 1); }; return; }; capitalize(arr); console.log(arr);
Output
And the output in the console will be −
[ 'Apple', 'Banana', 'Orange', 'Grapes' ]
- Related Articles
- How to capitalize the first letter of each word in a string using JavaScript?
- Write a Java program to capitalize each word in the string?
- Counting elements of an array using a recursive function in JS?
- Using merge sort to recursive sort an array JavaScript
- Java Program to Capitalize the first character of each word in a String
- Python program to capitalize each word's first letter
- Capitalize a word and replace spaces with underscore - JavaScript?
- Capitalize letter in a string in order and create an array to store - JavaScript
- Sum of even numbers up to using recursive function in JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Recursive multiplication in array - JavaScript
- How to make a function that returns the factorial of each integer in an array JavaScript
- Recursive program to find an element in an array linearly.
- How to use my object like an array using map function in JavaScript?
- How to filter values from an array using the comparator function in JavaScript?

Advertisements