[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.
[javascript] reduce(), repeat()
1. reduce(callbackFn, initialValue) 2. repeat(count) function solution(heart, k) { let answer = []; heart.map((a) => { const expanded = [...a].reduce((acc, cur) => acc + cur.repeat(k), ''); for (let i = 0; i < k; i++) answer.push(expanded); }); return answer; } let heart = ['.oo...oo.', 'o..o.o..o', 'o...o...o', '.o.....o.', '..o...o..', '...o.o...', '....o....']; console.log('heart', heart); co..
2023. 8. 10.
[javascript] 절대 값 Math.abs()
function solution(num1, num2) { return Math.abs(num1 - num2); } let num1 = [21, 23, 27, 30, 33, 43]; let num2 = [2, 7, 21, 28, 34, 38]; for (let i = 0; i < num1.length; i++) { console.log('solution', solution(num1[i], num2[i])); } 결과
2023. 8. 9.