错题3 判断能力分析

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
}
 

 当做一个复杂步骤的函数的时候。我们可以分步骤做

1,先判断是否为9

2,为九的话四周下标各加一

3.判断加一的函数是够满足条见(不在坐标外,不等于9)

 

如果是我之前思路

1,判断是不是等于九

2.不等于九,遍历四周数字(遍历的是否判断是不是过边界),为九的中间数字加一

//我的思路是连续性的,没有代码模块概念

//分工一定要明确,哪怕多写几行代码。思路清晰,好调整。可视化高

const plus = function (re, a, b) {
//     console.log(a,b)
    if (a >= 0 && a < 4 && b >= 0 && b < 4 && re[a][b] !== 9) {
        console.log("come",a, b)
        re[a][b] += 1
    }
}

    const markAround = function (re, el, x , y) {
        if (el === 9) {
            plus(re, x - 1, y + 1)
            plus(re, x - 1, y)
            plus(re, x - 1, y - 1)
 x, y
            plus(re, x, y + 1)
            plus(re, x, y - 1)
 x, y
            plus(re, x + 1, y + 1)
            plus(re, x + 1, y)
            plus(re, x + 1, y - 1)
        }
    }

    const markedSquare = function(array) {
        console.log("start")
        var re = array.slice(0)
        for (let i = 0; i < re.length; i++) {
            var x = i
            for (let j = 0; j < re.length; j++) {
                var y = j
                var el = re[x][y]
                markAround(re, el, x, y)

            }
        }
        console.log(re)
        return re
    }

    let array = [
        [0, 9, 0, 0],
        [0, 0, 9, 0],
        [9, 0, 9, 0],
        [0, 9, 0, 0],
    ]
    let r = markedSquare(array)
    console.log('result', r)

 

posted @ 2019-10-21 18:22  容忍君  阅读(195)  评论(0编辑  收藏  举报