在 JavaScript 中破坏驼峰式命名语法
javascriptweb developmentfront end technology
问题
我们需要编写一个 JavaScript 函数,该函数接受驼峰式命名字符串 str 作为第一个也是唯一的参数。
我们的函数应该构造并返回一个新字符串,该字符串使用单词之间的空格来分割输入字符串。
例如,如果函数的输入是 −
输入
const str = 'thisIsACamelCasedString';
输出
const output = 'this Is A Camel Cased String';
示例
以下是代码 −
const str = 'thisIsACamelCasedString'; const breakCamelCase = (str = '') => { const isUpper = (char = '') => char.toLowerCase() !== char.toUpperCase() && char === char.toUpperCase(); let res = ''; const { length: len } = str; for(let i = 0; i < len; i++){ const el = str[i]; if(isUpper(el) && i !== 0){ res += ` ${el}`; continue; }; res += el; }; return res; }; console.log(breakCamelCase(str));
输出
this Is A Camel Cased String