美文网首页
字符串---单词反转,大写字母位置不变

字符串---单词反转,大写字母位置不变

作者: VickyFan | 来源:发表于2018-08-07 10:04 被阅读0次
/**
 *解题思路:
 *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))

相关文章

网友评论

      本文标题:字符串---单词反转,大写字母位置不变

      本文链接:https://www.haomeiwen.com/subject/dylovftx.html