function solution(num1, num2) {
var answer = parseInt(num1 / num2)
return answer;
}
❗️ 여기서 몫을 구할 때 parseInt사용!, 나머지를 구할때는 % 사용
function solution(arr) {
const result = arr.reduce((a,b) => (a+b))
return result / arr.length
}
// const sumWithInitial = array1.reduce(
// (accumulator, currentValue) => accumulator(축적된값) + currentValue(현재값),
// initialValue
);
const n = 12
function solution(n) {
const newArray = Array(n).fill().map((v,i)=>i+1)
const measure = newArray.map((item)=> {
if(n % item === 0 ){
return item
}
})
const notUndefined = measure.filter(el => el !== undefined)
const result = notUndefined.reduce((a,b)=> a+b)
return result
}
// 해설
// 1)
// Array(number) 12개 공백이 인자로 들어간 배열이 만들어짐, 배열에 암것도 없지만 length가 12임
// Array(number)
// 결과값: [ <12 empty items> ]
// 2)
// fill()는 배열의 시작 ~ 끝 인덱스 전까지 정적인 값으로 채우는 함수
// Array(number).fill()
// 결과값: [ undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined ]
// 3)
// map() 반복문
// Array(number).fill().map((v,i)=>i+1)
// 결과값: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
// -> 여기까지는 0~n의 길이만큼 배열로 만들기
//4)
// measure은 map을 돌면서 받은 n을 돌아가면서 나눔 그리고 나머지가 0이면 n의 약수라서 나머지가 0인 애들을 return
// 결과값: [1,2,3,4,undefined,6,undefined,undefined,undefined,undefined,undefined,12]
// 5)
// notUndefined는 배열에서 undefined 제거
// 6)
// result는 reduce 함수를 사용해서 최종 결과값을 순회하면서 더해줌!
3번 문제는... newArray 부분에서 undefined 제거를 뭔가 할 수 있을 것 같은데... 못 찾은게 너무 아쉬웠다! 그래도 처음부터 끝까지 내 힘으로 알고리즘 풀었다 무야호! 마지막 문제는 물론 40분걸린듯 ㅎㅎ...