A function that capitalizes the first three letters of each word in the given array.
<p>I wrote a function that takes a word and capitalizes the first three letters. Now I need to run the same function on an array of words to return the first three letters of each word in upper case. I see a lot of people asking how to capitalize the first letter of every word in a sentence, but it's not the same thing. I have to use a function I've already written so that when I print it using console.log, its output looks like this: </p>
<pre class="brush:php;toolbar:false;">console.log(applyAll(['str1', 'str2', 'str3', 'str4'], capitalizeThreeLetters));</pre>
<p>I tried using a for loop to achieve this, but it returned the result of all the words concatenated. In my research I saw that you can use the forEach() method to run a function on array elements, but I can't figure out how to apply it. </p>
<pre class="brush:php;toolbar:false;">//Function that takes in str returns it with three first letters capitalized
function capitalizeThreeLetters(str){
let capFirst = str[0].toUpperCase();
let capSecond = str[1].toUpperCase();
let capThird = str[2].toUpperCase();
let splitStr = str.slice(3);
let wholeStr = capFirst capSecond capThird splitStr;
return wholeStr;
}
console.log(capitalizeThreeLetters('testing')); // => returns 'TESting'
console.log(capitalizeThreeLetters('again')); // => returns 'AGAin'
//Function that takes in a string array and applies capitalizeThreeLetters function to each array element so each word is returned with first three letters capitalized
function applyAll(arr){
for (let i = 0; i < arr.length; i ){
return capitalizeThreeLetters(arr);
}
}
console.log(applyAll(['mai', 'brian', 'jeho', 'han'], capitalizeThreeLetters));
// => returns 'MAIBRIANJEHOhan'
// => should return ['MAI', 'BRIan', 'JEHo', 'HAN']</pre>
<p><br /></p>