[프로그래머스 lev1/JS] 내적

woolee의 기록보관소·2022년 11월 3일
0

알고리즘 문제풀이

목록 보기
40/178

문제 출처

프로그래머스 lev1 - 내적

문제

나의 풀이

function solution(a, b) {
  let ans=0;
  for (let i=0; i<a.length; i++) {
    ans +=a[i]*b[i];
  }
  return ans;
}

console.log(solution([1, 2, 3, 4], [-3, -1, 0, 2]));

다른 풀이

function solution(a, b) {
  // return a.reduce((acc, _, i) => acc += a[i] * b[i], 0);
  return a.reduce((acc, cur, idx) => acc += cur*b[idx], 0)
}

console.log(solution([1, 2, 3, 4], [-3, -1, 0, 2]));
profile
https://medium.com/@wooleejaan

0개의 댓글