DAY30:Mean Square Error


Posted by birdbirdmurmur on 2023-08-12

題目連結

https://www.codewars.com/kata/51edd51599a189fe7f000015

解法

function solution(firstArray, secondArray){
  let sum = 0;
  for(let i = 0; i < firstArray.length; i++){
    sum += (firstArray[i] - secondArray[i]) ** 2;
  }
  return sum / firstArray.length;
}

筆記

一開始使用雙層迴圈
後來發現兩個array長度一樣
不需要多一個迴圈
同一個index下的數字差
這邊沒有判斷正負
因為平方後一定是正數
最後回傳前 除上陣列長度


#javascript #Codewars #number







Related Posts

[Web] ASP.Net 4.8 WebApi (Swagger, Jwt)

[Web] ASP.Net 4.8 WebApi (Swagger, Jwt)

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段三:個人專案前準備~Dive into topics)

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段三:個人專案前準備~Dive into topics)

Node.js

Node.js


Comments