简体   繁体   English

代码重构。 试图改进我的代码

[英]Code Refactoring. Trying to improve my code

My code passed, no problem.我的代码通过了,没问题。 But I would like your guys opinion as to what I could have improved in my code.但是我希望你们对我可以在代码中改进的地方发表意见。 Unnecessary things, tips, better ways to do the same thing, faster ways, I'm literally open to any kind of feedback.不必要的事情,技巧,做同样事情的更好方法,更快的方法,我真的很乐意接受任何类型的反馈。 Lately I'm only trying to focus on improve how fast I can solve a problem, and this one, took me almost 5 hours.最近我只是想专注于提高我解决问题的速度,而这个问题花了我将近 5 个小时。 This code comes from the 2D Array HourGlass.此代码来自 2D Array HourGlass。 My thought process was to makeup a model of what I wanted, than for loop through the lines and rows, and that's how I came with this result.我的思考过程是建立一个我想要的模型,而不是循环遍历行和行,这就是我得到这个结果的方式。 Also, I wanted to improve from thinking of WHAT the code should do, other than HOW.此外,我想通过思考代码应该做什么来改进,而不是如何做。 It's hard, but any tips I would really appreciate.这很难,但我真的很感激任何提示。 Since I'm coding only Front End stuff, my solving problems is literally shit.因为我只编码前端的东西,所以我解决的问题简直是狗屎。
Thanks !谢谢 !

function hourglassSum(arr) {

        let newInput = arr
        let arrAnswer = []

        for(let line in newInput){
            for (let row in newInput){
                let newRow = parseInt(row)
                let newLine = parseInt(line)
                if(newLine < 4){
                    let a =newInput[newLine +0][newRow]
                    let b =newInput[newLine +0][newRow+1]
                    let c =newInput[newLine +0][newRow+2]
                    let d =newInput[newLine +1][newRow+1]
                    let e =newInput[newLine +2][newRow]
                    let f =newInput[newLine +2][newRow+1]
                    let g =newInput[newLine +2][newRow+2]
                    if(a,b,c,d,e,f,g == undefined){
                        break
                    }
                    arrAnswer.push([a,b,c,d,e,f,g].reduce((item1,item2)=> item1 + item2, 0))
                }
            }
        }

        let answer = arrAnswer.reduce((item1, item2) => (item1 > item2 ) ? item1: item2 )

        return answer 

    }

if(a,b,c,d,e,f,g == undefined) Are you expecting this to check if any of your 7 values are undefined? if(a,b,c,d,e,f,g == undefined)您是否希望它检查您的 7 个值中是否有任何一个未定义?

Based on the comma operator specs I believe it is only checking g == undefined .根据逗号运算符规范,我相信它只是检查g == undefined

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.逗号运算符计算其每个操作数(从左到右)并返回最后一个操作数的值。

If you really mean to check for any null values, here's one way you could do it如果你真的想检查任何空值,这是你可以做到的一种方法

if([a,b,c,d,e,f,g].indexOf(undefined)>=0) ...

Your code has a lot of redundancies:你的代码有很多冗余:

let newInput = arr

Unnecessary.不必要。

let answer = arrAnswer.reduce((...

Stuffing it in a var is unnecessary, since you just return it on the next line.将它填充到 var 中是不必要的,因为您只需在下一行返回它。

As far as I can tell, your entire code can be changed to the following:据我所知,您的整个代码可以更改为以下内容:

const hourglassSum = input => {
  return input
    .map((a, i, arr) => { // NEVER use for..in with arrays. Use .map or for..of
      return arr.map(b => {
        const idx1 = parseInt(a, 10); // always use radix
        const idx2 = parseInt(b, 10);

        // Use boolean short-circuiting, we'll filter later.
        // Your original code had potentially error throw here
        // if the first access resulted in undefined.
        const intermediate = input[idx1] !== undefined &&
          input[idx1 + 1] !== undefined &&
          input[idx1 + 2] !== undefined &&
          [
            input[idx1][idx2],
            input[idx1][idx2 + 1],
            input[idx1][idx2 + 2],
            input[idx1 + 1][idx2 + 1],
            input[idx1 + 2][idx2],
            input[idx1 + 2][idx2 + 1],
            input[idx1 + 2][idx2 + 2],
          ];

        // boolean again, check to make sure we don't pollute the
        // arithmetic 
        return intermediate &&
          intermediate.every(x => x !== undefined) &&
          intermediate;
      })
      .filter(x => x) // weed out falses
      .reduce((a, b) => a + b, 0); // sum to int
    })
    .reduce((a, b) => Math.max(a, b)); // Math.max replaces ternary
};

This is arguably more readable, definitely less error prone, slightly shorter, makes better use of the built-ins like Math.max and the array methods.这可以说是更具可读性,绝对不容易出错,略短,更好地利用内置函数,如Math.max和数组方法。 Is also consistent rather than mixing functional style with loops.也是一致的,而不是将功能风格与循环混合。 One thing it isn't is faster, but you make it correct first, then fast.有一件事不是更快,但你先让它正确,然后快速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM