写一个名为toCapitalize的方法,实现首字母大写功能(原来字母就是大写的不处理),如’i like css’转换成’I Like Css’。
let str = 'i like css'
function toCapitalize(lowStr) {
let arr = lowStr.split('')
let arr2 = [] // 新字符串数组
let toUpIndex = null // 存一下要变成大写的index
arr.forEach((ele, index) => {
if (index === toUpIndex) {
arr2.push(ele.toUpperCase())
}
if (ele === ' ') {
arr2.push(ele)
toUpIndex = index + 1
} else if (index === toUpIndex) {
} else {
arr2.push(ele)
}
})
console.log(arr2.join(''))
}
toCapitalize(str) // i Like Css
新手上路,非标准答案,请多指教