0

For example, for the input "there is a newbie", the output should be "There Is A Newbie".

function titleCase(str) {
    let arr = str.toLowerCase().split(" ");
    let secondArr = [];
    for (let i = 0; i<arr.length; i++) {
        let subArr = [];
        for (let j = 0; j<arr[i].length; j++) {
            if (**arr[i][0] == arr[i][j]**) { // I need this condition to be corrected, so that whenever it runs first letter of a word, it makes it the upper case.
                subArr.push(arr[i][j].toUpperCase());
            } else {
                subArr.push(arr[i][j].toLowerCase());
            }
        }
        secondArr.push(subArr.join(""));
    }
    return secondArr.join(" ");
}
titleCase("You are a little newbie"); // it has to return "You Are A Little Newbie"
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 2
    possible duplicate of : [How to capitalize the first letter of each word in a string using JavaScript](https://stackoverflow.com/questions/32589197/how-to-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript) – anehme Jul 21 '20 at 18:38

1 Answers1

1

You need to capitalize only the first letter and then append the rest of the string. Your code can be improved by splitting on any repeated whitespace with the regular expression \s+ and using Array#map to simplify it.

function titleCase(str) {
  return str.toLowerCase().split(/\s+/)
    .map(s=>s[0].toUpperCase() + s.slice(1)).join(' ');
}
console.log(titleCase("You are a little newbie"));

Your current code does not require a nested loop; you only need to loop over each part of the array and push the string with the first letter capitalized to the result array.

function titleCase(str) {
  let arr = str.toLowerCase().split(" ");
  let res = [];
  for (let i = 0; i<arr.length; i++) {
    let curr = arr[i];
    res.push(curr[0].toUpperCase() + curr.slice(1));
  }
  return res.join(" ");
}
console.log(titleCase("You are a little newbie"));

If you really want to continue with your method, you need only check that j is 0 to know that it is the first letter of a word and should be capitalized.

function titleCase(str) {
    let arr = str.toLowerCase().split(" ");
    let secondArr = [];
    for (let i = 0; i<arr.length; i++) {
        let subArr = [];
        for (let j = 0; j<arr[i].length; j++) {
            if (j === 0) {
                subArr.push(arr[i][j].toUpperCase());
            } else {
                subArr.push(arr[i][j].toLowerCase());
            }
        }
        secondArr.push(subArr.join(""));
    }
    return secondArr.join(" ");
}
console.log(titleCase("You are a little newbie"));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Thank you. But is there a way to correct my code, I mean the line that I have a comment on? I tried to make a logic like **if (arr[i][j] == to the first index of the string (THEN) push it to the array as a capital letter" – coder online Jul 21 '20 at 18:40
  • I'm really sorry for being a pain in the ass, but still if my way of writing the code was the only one, how could I tell the computer to do that thing on that line that if arr[i][j] is the first element i.e. the index zero, then do it uppercase – coder online Jul 21 '20 at 18:47
  • Just for information, in Swift you have the capitalised property : print("You are a little newbie".capitalized) => You Are A Little Newbie ; and print("You are a little NEWBIE".capitalized) gives the same => You Are A Little Newbie – claude31 Jul 21 '20 at 20:29
  • @hev1 I'm happy to do that, but don't know how. How do I accept your answer? I'm a newbie here, it is my first post so far – coder online Jul 22 '20 at 11:24
  • 1
    @hev1 does it show now that I've accepted your answer? just making sure that I didn't leave you without "thank you" thing. Thank you very much with showing what mistake I had, now it seems so easy and apparent, I had to think harder :D – coder online Jul 22 '20 at 11:31