简体   繁体   English

断骆驼案例 function in JavaScript

[英]Break Camel Case function in JavaScript

I have been attempting to solve this codewars problem for a while in JavaScript:我一直试图在 JavaScript 中解决这个 codewars 问题一段时间:

"Complete the solution so that the function will break up camel casing, using a space between words. Example:" “完成解决方案,以便 function 将打破驼峰式外壳,在单词之间使用空格。示例:”

"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.第一次插入newStr将在正确的位置,但在插入空格之后,在newStr中跟随它的字母将处于增加的索引处。 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 .这意味着当在splitStr中的i处找到下一个大写字母时(未更改),插入newStr确实更改了)实际上应该在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. [AZ]的大写字母。
  • $& backreference to the matched letter, used in the replacement. $&反向引用匹配的字母,用于替换。
  • g global flag so all matches are replaced. g全局标志,因此所有匹配项都被替换。

Here could be a solution with a simple loop and some if conditions这可能是一个带有简单循环和一些 if 条件的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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