[javascript] 연속된 숫자의 합, new Array(), fill(), map()
공차가 1인 등차수열의 합 : sum =n(2a+n-1)/2 a: 첫째항, n:항의갯수 a = sum/n -(n -1)/2 function solution1(num, total) { let a = total / num - (num - 1) / 2; return new Array(num).fill().map((e, i) => { return i + a; }); } let num = [3, 5, 4, 5]; let total = [12, 15, 14, 5]; for (let i = 0; i < num.length; i++) { console.log('solution1', solution1(num[i], total[i])); } 결과
2023. 8. 24.