Code works, Still can't progress

Code works, Still can't progress
0

#1

Tell us what’s happening:
The code works the way its supposed to, at least as far as i can tell, but it wont let me progress. Any ideas?

Your code so far


function titleCase(str) { 
    let string = str.toLowerCase();
    let arr = string.split(' ');
    let newString = [];
    for(let i = 0; i < arr.length; i++){
        newString.push( arr[i].replace((arr[i].charAt(0)), (arr[i].charAt(0).toUpperCase())) + " ");
    }
    return newString.toString().replace(/,/g, '');

 }
  
let n = titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
console.log(n)  

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence


#2

Hi @notSatan.

You’re adding an extra whitespace in your for loop at the end of each sentence.

So with your code: "HERE IS MY HANDLE HERE IS MY SPOUT" becomes "Here Is My Handle Here Is My Spout ".

What I suggest is that you remove the whitespace you add in the for loop. And instead of replacing the commas with '', you replace them with a whitespace:

function titleCase(str) { 
    let string = str.toLowerCase();
    let arr = string.split(' ');
    let newString = [];
    for(let i = 0; i < arr.length; i++){
        newString.push( arr[i].replace((arr[i].charAt(0)), (arr[i].charAt(0).toUpperCase())));
    }
    return newString.toString().replace(/,/g, ' ');

 }
  
let n = titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
console.log(n)  

It should work that way.


#3

Thanks! I ended up just copying the answer code to move on, as I understood the concept trying to be put across, but going back and trying the modification worked as well. Sometimes the passing qualifications can be irritatingly fussy.