简体   繁体   中英

Break Camel Case function in JavaScript

I have been attempting to solve this codewars problem for a while in JavaScript:

"Complete the solution so that the function will break up camel casing, using a space between words. Example:"

"camelCasing"  =>  "camel Casing"

"identifier"   =>  "identifier"

""             =>  ""

I have it almost all the way, but for some reason my code is selecting the wrong space to add a blank space. I'm hoping someone can tell me what I am doing wrong.

 function solution(string) { let splitStr = string.split(""); let newStr = string.split(""); let capStr = string.toUpperCase().split(""); for (i = 0; i < splitStr.length; i++) { if (splitStr[i] === capStr[i]) { newStr.splice(i, 0, ' '); } } return newStr.join(""); } console.log('camelCasing: ', solution('camelCasing')); console.log('camelCasingTest: ', solution('camelCasingTest'));

The first insertion into newStr will be at the correct spot, but after that insertion of the space, the letters that follow it in newStr will be at an increased index. This means that when the next capital is found at i in splitStr (which did not change), the insertion into newStr (which did change) should really be at i+1 .

A solution is to make your loop iterate from end to start:

 function solution(string) { let splitStr = string.split(""); let newStr = string.split(""); let capStr = string.toUpperCase().split(""); for (i = splitStr.length - 1; i >= 0; i--) { if (splitStr[i] === capStr[i]) { newStr.splice(i, 0, ' '); } } return newStr.join(""); } console.log('camelCasing: ', solution('camelCasing')); console.log('camelCasingTest: ', solution('camelCasingTest'));

This kind of problem is however much easier solved with a regular expression:

 function solution(string) { return string.replace(/[AZ]/g, " $&"); } console.log('camelCasing: ', solution('camelCasing')); console.log('camelCasingTest: ', solution('camelCasingTest'));

Explanation of the regular expression:

  • [AZ] a capital letter from the Latin alphabet.
  • $& backreference to the matched letter, used in the replacement.
  • g global flag so all matches are replaced.

Here could be a solution with a simple loop and some if conditions

const breakCamelCase = (word) => {
  let result = "";
  // loop on letter
  for (let letter of word) {
    // if letter is uppercase and not the first letter of the word add a space followed by the letter
    if (letter == letter.toUpperCase() && result) {
      result += ` ${letter}`;
    } else { // else just add the letter
      result += letter;
    }
  }
  return result;
}

 function solution(string) { let splitStr = string.split(""); let newStr = ""; splitStr.forEach(e =>{ if(e === e.toUpperCase()) newStr +=" "+e; else newStr += e; }); return newStr; } console.log(solution('camelCasing'));//success = "camel Casing" console.log(solution('camelCasingTest'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM