
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Capitalize Letter in a String and Create an Array in JavaScript
We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive captial letters in every word −
For example −
If the string is −
const str = 'edabit';
Then the output should be the following i.e. successive single capital letter −
const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"];
Example
Following is the code −
const str = 'edabit'; const replaceAt = function(index, char){ let a = this.split(""); a[index] = char; return a.join(""); }; String.prototype.replaceAt = replaceAt; const createEdibet = word => { let array = word.split('') const res = array.map((letter, i) => { let a = word.replaceAt(i, letter.toUpperCase()); return a; }); return res; } console.log(createEdibet(str));
This will produce the following output on console −
[ 'Edabit', 'eDabit', 'edAbit', 'edaBit', 'edabIt', 'edabiT' ]
Advertisements