1. 프로그래머스
Lv1. 부족한 금액 계산하기
const solution=(price, money, count)=> {
let totalPrice = 0;
for(let i=1; i<=count; i++){
totalPrice += price*i
}
if(totalPrice >= money){
return totalPrice - money
}
else{
return 0
}
}
function solution(price, money, count) {
const tmp = price * count * (count + 1) / 2 - money;
return tmp > 0 ? tmp : 0;
}
1 + 2 + 3 + ... + n = n * (n + 1) / 2