1

Im working on an algorithm to return the provided string with the first letter of each word capitalized. While leaving the rest of the word in lower case. Looking for some insight to why this code isn't working. Thanks.

function titleCase(str) {
    let result = "";
    let words = str.split(" ");


    for(let i = 0; i <= words.length; i++){
    let word = words[i];

    for(let j = 0; j <= word.length; j++){

    if(j === 0){
    result += word[j].toUpperCase();

    } else{
      result += word[j].toLowerCase();
    }
  }
      return result += " "
  }

      return result.slice(0, result.length - 1)
  }
4
  • 1
    ⟼This code could benefit greatly by adopting an indentation style and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. Commented Nov 14, 2020 at 1:35
  • 1
    Tip: This is a lot easier to do with a regular expression and replace with a function that focuses on each word to do the operation. A simpler approach is to use the split(" ").map(word => ...).join(" ") pattern. There's really no reason to do a for loop and then fumble around with let word = words[i]. Remember: forEach exists. Commented Nov 14, 2020 at 1:36
  • 1
    Try not to say "doesn't work", instead explain what happens and if you get errors please include the exact error text in your question. Commented Nov 14, 2020 at 1:39
  • 1
    Ok I will remember these gems for next time, thank you! Commented Nov 14, 2020 at 4:30

1 Answer 1

4

There are three errors in your program, that are stopping it from working:

  1. You need to change i <= words.length to i < words.length (or i <= words.length - 1), since indexes start from 0 in javascript like most programming languages.
  2. You need to change j <= word.length to j < word.length (or j <= word.length - 1), same reason as above.
  3. You need to not return prematurely, and need to change return result += " " to just result += " ".
  • Also although it does not stop your function from working you could simplify your return line by utilizing trimEnd.

function titleCase(str) {
    let result = "";
    let words = str.split(" ");

    for (let i = 0; i < words.length; i++) {
        let word = words[i];
        for (let j = 0; j < word.length; j++) {
            if (j === 0) {
                result += word[j].toUpperCase();
            } else {
                result += word[j].toLowerCase();
            }
        }
        result += " "
    }
    
    return result.trimEnd();
} 


console.log(titleCase("The quick brown fox jumps over the lazy dog"));

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.