错题3 判断能力分析

こ雲淡風輕ζ 提交于 2019-12-01 19:43:32
const markedLine = function(array) {
    for (let i = 0; i < array.length; i++) {
        if (array[i] === 9) {    // 每次只判断一项!!很浪费判断属性
            if (i - 1 >= 0) {
                if (i + 1 < array.length) {
                     array.splice(i - 1, 1, array[i - 1] + 1)
                     array.splice(i + 1, 1, array[i + 1] + 1)
                } else {
                    array.splice(i - 1, 1, array[i - 1] + 1)
                }
            } else {
                array.splice(i + 1, 1, array[i + 1] + 1)
            }
        }
    }
    log(array)
    return array
}
const markedLine = function(array) {
 
    let line = array.slice(0)    for (let i = 0; i < line.length; i++) {        let e = line[i]        // 如果 e 是 9, 左边 + 1        if (e === 9 && i > 0) {     //和我一开始的思路一样 不过后来我失败了!这个代码看着舒服!左边是左边!右边是右边            if (line[i - 1] !== 9) {                line[i - 1] += 1            }        }        // 如果 e 是 9, 右边 + 1        if (e === 9 && i < line.length - 1) {            if (line[i + 1] !== 9) {                line[i + 1] += 1            }        }    }    return line}
 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!