반응형 foreach1 [javascript] 배열 원소들의 합 1. for 문 : for ([initialization]; [condition]; [final-expression]) statement function solution(n) { let answer = 0; for (let i = 0; i < n.length; i++) { answer += n[i]; } return answer; } const n = [8, 16, 27, 32, 81]; console.log('solution', solution(n)); 결과 2. forEach 문 arr.forEach(callback(currentvalue[, index[, array]])[, thisArg]) function solution(n) { let answer = 0; n.forEach((element) =.. 2023. 7. 13. 이전 1 다음 반응형