Capitalize letter in a string in order and create an array to store - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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' ]
raja
Published on 01-Oct-2020 10:22:49
Advertisements