1

This simple program I am making, solution, isn't returning the intended output.

If the string includes the phrase camelCasing the expected output should be camel Casing. If the string includes any other words like camelCasingCarrier the output should be camel Casing Carrier.

If the string includes any other phrase, it should just return the string.

function solution(string) {
  let newString = null;
  let stringToAdd = ' '
  if (string.includes('camelCasing')) {
    for (let i = 0; i < string.length; i++) {
      let letter = string[i];
      if (letter == letter.toUpperCase()) {
        newString = string.substring(0, letter) + stringToAdd + string.substring(letter);
      }
    }
    return newString;
  } else {
    return string;
  }
}

My current output is camelCasingCarrier with a space at the beginning rather than the expected output. What is my issue and how should I fix this? Thanks again :)

7
  • 1
    Each time through the loop you overwrite the changes to newString that you made on the previous iteration. Commented Feb 16, 2022 at 20:06
  • You need to accumulate characters in newString, not overwrite it completely. Commented Feb 16, 2022 at 20:07
  • I see I see, should I then accumulate characters into an array and then join them together into a string? @Barmar Commented Feb 16, 2022 at 20:11
  • string.substring takes numeric arguments, but letter is a letter. Maybe you meant to use i? Commented Feb 16, 2022 at 20:19
  • You can accumulate into a string. Initialize newString = '' then use newString += to add to it. Commented Feb 16, 2022 at 20:20

1 Answer 1

1

The substring method string.substring(start, end) takes up to two arguments, integer numbers that must be the index range of the string, to generate a substring from. In your case you are using a letter (letter) as an argument and this won't work. If you use the index i instead of letter it would work, but only for a single replacement and not for "camelCasingCarrier" for example.

As you already mentioned, the best solution is to avoid using the substring method and simply add a space before the capital letters as you parse over the string:

function solution(string) {
  let newString = '';
  if (string.includes('camelCasing')) {
    for (let i = 0; i < string.length; i++) {
      let letter = string[i];
      if (letter == letter.toUpperCase()) {
        newString += ' ' + letter;
      } else {
        newString += letter;
      }
    }
    return newString;
  } else {
    return string;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer would be better if you called attention to what the original problem was and how your solution fixes the problem.
Thank you, I added more info.

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.