Problem Solution/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JS] ์˜ท๊ฐ€๊ฒŒ ํ• ์ธ ๋ฐ›๊ธฐ

yuri lee 2023. 8. 29. 22:41
๋ฐ˜์‘ํ˜•

Problem Description

๋จธ์“ฑ์ด๋„ค ์˜ท๊ฐ€๊ฒŒ๋Š” 10๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 5%, 30๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 10%, 50๋งŒ ์› ์ด์ƒ ์‚ฌ๋ฉด 20%๋ฅผ ํ• ์ธํ•ด์ค๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•œ ์˜ท์˜ ๊ฐ€๊ฒฉ price๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ง€๋ถˆํ•ด์•ผ ํ•  ๊ธˆ์•ก์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

 

Restrictions

  • 10 ≤ price ≤ 1,000,000 price๋Š” 10์› ๋‹จ์œ„๋กœ(1์˜ ์ž๋ฆฌ๊ฐ€ 0) ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 
  • ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฐ ์ •์ˆ˜๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

 

Input/Output Example

  • ์ž…์ถœ๋ ฅ ์˜ˆ #1 150,000์›์—์„œ 5%๋ฅผ ํ• ์ธํ•œ 142,500์›์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2 580,000์›์—์„œ 20%๋ฅผ ํ• ์ธํ•œ 464,000์›์„ return ํ•ฉ๋‹ˆ๋‹ค.

 

My solution

function solution(price) {
    if (price >= 500000) {
        return Math.floor(price - (price * 0.20));
    } else if (price >= 300000) {
        return Math.floor(price - (price * 0.10));
    } else if (price >= 100000) {
        return Math.floor(price - (price * 0.05));
    } else {
        return price;
    }
}

https://school.programmers.co.kr/learn/courses/30/lessons/120818

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

 

Math.floor() - JavaScript | MDN

The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.

developer.mozilla.org

 

๋ฐ˜์‘ํ˜•