Tell us what’s happening:
When I run console.log(titleCase("hello WORld"));
, it logs out Hello World
, but I’m not passing any of the four tests except for the first one. Is it because I’m returning newStr instead of str?
Your code so far
function titleCase(str) {
// Make an array of subarrays containing the characters of each word of str
let strArr = str.split(' ').map(v => v.split(''));
// Initialize a new string to output
let newStr = "";
// Make the first letter of every subarray capital and the rest lowercase
strArr = strArr.map(v => v.map((v, i) => i === 0 ? v.toUpperCase() : v.toLowerCase()));
// Push the letters of each subarray to the console followed by a space
strArr.forEach(v => {
v.forEach(v => {
newStr += v;
});
newStr += " " ;
});
// Return the new string
return newStr;
}
console.log(titleCase("hello WORld"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/