[프로그래머스/JS] LV.0 - 옷가게 할인 받기

JS·2023년 3월 3일
0

알고리즘

목록 보기
17/26

🏄‍문제

머쓱이네 옷가게는 10만 원 이상 사면 5%, 30만 원 이상 사면 10%, 50만 원 이상 사면 20%를 할인해줍니다.
구매한 옷의 가격 price가 주어질 때, 지불해야 할 금액을 return 하도록 solution 함수를 완성해보세요.

🕐제한사항

  • 10 ≤ price ≤ 1,000,000
  • price는 10원 단위로(1의 자리가 0) 주어집니다.
    소수점 이하를 버린 정수를 return합니다.

👨‍💻입출력 예 설명

priceresult
150,000142,500
580,000464,000

입출력 예 #1

  • 150,000원에서 5%를 할인한 142,500원을 return 합니다.

입출력 예 #2

  • 580,000원에서 20%를 할인한 464,000원을 return 합니다.

🧐나의 풀이

function solution(price) {
  if (price >= 100000 && price < 300000) {
    return Math.floor(price * 0.95);
  }

  if (price >= 300000 && price < 500000) {
    return Math.floor(price * 0.9);
  }

  if (price >= 500000) {
    return Math.floor(price * 0.8);
  }

  return price;
}

🥳다른 사람의 풀이

//1 
function solution(numbers) {
  var answer = 0;
  for (i of numbers) {
    answer += i;
  }
  return answer / numbers.length;
}

//2  
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;
};
profile
신입 FE 개발자

0개의 댓글

관련 채용 정보