/**
*解题思路:
*1.现将字符串split切割成数组['Many', 'people', 'spell', 'MySQL', 'incorrectly']
*2.map遍历数组,将每个单词反转并都取小写
*3.查看原来的单词第几位是大写,同样改变后来的单词位置
*4.最后将改变大小写后的数组join成单词,再放置到新数组中
*/
let string = "Many people spell MySQL incorrectly";
/**
* [convert 反转排序]
* @param {[String]} string [字符串]
*/
function convert (string) {
let arr = string.split(' ')
let newArr = []
arr.map((item1,index1) => {
let word = item1.split('')
word = word.reverse();
for (let i = 0;i < word.length; i++){
word[i] = word[i].toLowerCase();
if(IsUpper(item1.split('')[i])){
word[i] = word[i].toUpperCase();
}
}
newArr.push(word.join(''))
})
return newArr
}
/**
* [IsUpper 判断字母是否大小写]
* @param {[String]} code [字母]
*/
function IsUpper(code) {
return code === code.toUpperCase()
}
console.log(convert(string))
网友评论