最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Break Camel Case function in JavaScript - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Sep 22, 2022 at 20:32 j08691 208k32 gold badges269 silver badges280 bronze badges asked Sep 22, 2022 at 20:21 Nicolette DeVilleNicolette DeVille 152 silver badges8 bronze badges 2
  • 1 This would be a lot simpler with map(). – tadman Commented Sep 22, 2022 at 20:33
  • Would love for you to show me as I am still trying to figure out how to use map(). – Nicolette DeVille Commented Sep 22, 2022 at 20:41
Add a ment  | 

4 Answers 4

Reset to default 6

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.

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

I e up with this:

function solution(string) {
  let str = "";
  for (let i = 0; i < string.length; i++) {
    str += /[A-Z]/.test(string[i]) ? ` ${string[i]}` : `${string[i]}`;
  }
  return str;
}
发布评论

评论列表(0)

  1. 暂无评论