프로그래머스 1단계 - 내적

원동휘·2022년 9월 24일
0

프로그래머스

목록 보기
22/46

< 문제 >

풀이

  • a와 b의 길이, 순서가 같다고 가정되어있기때문에 둘중의 하나의 length만큼 반복을 돌면서
    각각의 index의 해당하는 값을 더해서 풀이
function solution(a, b) {
  let answer = 0;
  for (let i = 0; i < a.length; i++) {
    answer = answer + a[i] * b[i];
  }
  return answer;
}

console.log(solution([1, 2, 3, 4], [-3, -1, 0, 2]));
console.log(solution([-1, 0, 1], [1, 0, -1]));
profile
Front-End Developer #Nextjs #React #Typescript

0개의 댓글