Home > Enterprise >  Break Camel Case function in JavaScript
Break Camel Case function in JavaScript

Time:09-23

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'));

CodePudding user response:

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(/[A-Z]/g, " $&");
}

console.log('camelCasing: ', solution('camelCasing'));
console.log('camelCasingTest: ', solution('camelCasingTest'));

Explanation of the regular expression:

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

CodePudding user response:

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;
}

CodePudding user response:

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'));

  • Related