1. 프로그래머스
Lv0. 옷가게 할인 받기
const solution=(price)=> {
let discountedPrice = 0;
if(price >= 500000){
discountedPrice = (price*0.80)
}
else if(price >= 300000){
discountedPrice = price*0.90
}
else if(price >= 100000){
discountedPrice = price*0.95
}
else{
return price
}
return Math.trunc(discountedPrice)
}
Math.trunc()
- 소수점 버린 정수 구하기const discounts = [
[500000, 20],
[300000, 10],
[100000, 5],
]
const solution = (price) => {
for (const discount of discounts)
if (price >= discount[0])
return Math.floor(price - price * discount[1] / 100)
return price
}
function solution(price) {
price = price>=500000?price*0.8:price>=300000?price*0.9:price>=100000?price*0.95:price;
return ~~(price);
}
~~
= double tilde 연산자로 알려진 비트 연산자Math.trunc()
함수 사용하기