簡體   English   中英

代碼重構。 試圖改進我的代碼

[英]Code Refactoring. Trying to improve my code

我的代碼通過了,沒問題。 但是我希望你們對我可以在代碼中改進的地方發表意見。 不必要的事情,技巧,做同樣事情的更好方法,更快的方法,我真的很樂意接受任何類型的反饋。 最近我只是想專注於提高我解決問題的速度,而這個問題花了我將近 5 個小時。 此代碼來自 2D Array HourGlass。 我的思考過程是建立一個我想要的模型,而不是循環遍歷行和行,這就是我得到這個結果的方式。 此外,我想通過思考代碼應該做什么來改進,而不是如何做。 這很難,但我真的很感激任何提示。 因為我只編碼前端的東西,所以我解決的問題簡直是狗屎。
謝謝 !

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)您是否希望它檢查您的 7 個值中是否有任何一個未定義?

根據逗號運算符規范,我相信它只是檢查g == undefined

逗號運算符計算其每個操作數(從左到右)並返回最后一個操作數的值。

如果你真的想檢查任何空值,這是你可以做到的一種方法

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

你的代碼有很多冗余:

let newInput = arr

不必要。

let answer = arrAnswer.reduce((...

將它填充到 var 中是不必要的,因為您只需在下一行返回它。

據我所知,您的整個代碼可以更改為以下內容:

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
};

這可以說是更具可讀性,絕對不容易出錯,略短,更好地利用內置函數,如Math.max和數組方法。 也是一致的,而不是將功能風格與循環混合。 有一件事不是更快,但你先讓它正確,然后快速。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM