๐ŸŸฃ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํšŒ๊ณ 

๊น€ํƒœ์šฑยท2023๋…„ 3์›” 2์ผ
0
post-thumbnail

๐Ÿซ  ์˜ค๋žœ๋งŒ์˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ง›๋งŒ ๋ณด์•˜๋‹ค.


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

๋ฌธ์ œ ์„ค๋ช…
๋จธ์“ฑ์ด๋„ค ์˜ท๊ฐ€๊ฒŒ๋Š” 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

๋‚˜์˜ ์ฒซ ํ’€์ด ์ฝ”๋“œ

function solution(price) { 
    if(price >= 100000) price * 0.05;
    else if(price >= 300000) price * 0.1;
    else if(price >= 500000) price * 0.2;

    return price
}

price์˜ ๋ฆฌํ„ด ๊ฐ’์ด ์—ฐ์‚ฐ๋˜์–ด ์žฌํ• ๋‹น ๋œ ๊ฐ’์ด ์•„๋‹Œ ๋Œ€์ž…ํ•œ ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋‚˜์˜ค๊ธฐ์— ๋” ์ด์ƒ์˜ ํ’€์ด๊ฐ€ ์–ด๋ ค์›Œ ๋‹ค๋ฅธ ์ด๋“ค์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋๋‹ค.

๋ฌธ์ œ์  1. if์˜ ์กฐ๊ฑด์ด ์ฐธ์ด ๋์„ ๋•Œ price์— ๊ฐ’์„ ์—ฐ์‚ฐ๋งŒ ํ•˜๊ณ  ์žฌํ• ๋‹น์„ ํ•˜์ง€ ์•Š์•„ ํ•จ์ˆ˜ ์ธ์ˆ˜์— ๋Œ€์ž…๋œ ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ด ๋˜์—ˆ์Œ.

	if(price >= 100000) price *= 0.05;
    else if(price >= 300000) price *= 0.1;
    else if(price >= 500000) price *= 0.2;

๊ทธ๋ ‡๊ฒŒ ์žฌํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๊ฒŒ *=์„ ๋”ํ•ด price์— ๊ณ„์‚ฐ์ด ์ ์šฉ๋œ ๊ฐ’์„ ์žฌํ• ๋‹น ํ•จ.

๋ฌธ์ œ์  2. ํ• ์ธ ๋œ ํผ์„ผํŠธ๊ฐ€ ์ ์šฉ๋œ ๋‚˜๋จธ์ง€ ๊ฐ’์ด ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋‚˜์™€์•ผ ํ•จ.

	if(price >= 100000) price *= 0.95;
    else if(price >= 300000) price *= 0.9;
    else if(price >= 500000) price *= 0.8;

ํ• ์ธ๋œ ๋‚˜๋จธ์ง€ ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ์ˆ˜์ •.

๋ฌธ์ œ์  3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŠน์„ฑ์ƒ ์œ„์—์„œ ์ฝ”๋“œ๋ฅผ ์ฝ์–ด๊ฐ€๋ฉฐ ํ•œ์ค„ ์”ฉ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์˜ ์ถฉ์กฑ์„ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•จ.

	if(price >= 500000) price *= 0.8;
    else if(price >= 300000) price *= 0.9;
    else if(price >= 100000) price *= 0.95;

์œ„์™€ ๊ฐ™์ด ์กฐ๊ฑด์˜ ๊ฐ’์„ ์ตœ๋Œ€ ๊ฐ’๋ถ€ํ„ฐ ์„ค์ •ํ•ด์•ผ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๋“ค์˜ ๊ฐ’์ด ์ˆœ์„œ๋Œ€๋กœ ์ ์šฉ๋˜์–ด ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•ด์ง. ์ตœ์†Œ๊ฐ’๋ถ€ํ„ฐ ์กฐ๊ฑด ์„ค์ •ํ•˜๊ฒŒ๋˜๋ฉด ์—ฐ์‚ฐ๋ผ์•ผ ํ•  ๊ฐ’๋“ค์ด ์ฒซ๋ฒˆ ์งธ ์กฐ๊ฑด์—์„œ ๋ฒ—์–ด๋‚˜์ง€ ๋ชป ํ•จ.

๋ฌธ์ œ์  4. ๋งˆ์ง€๋ง‰ ์ฑ„์ ์—์„œ ํ†ต๊ณผํ•˜์ง€ ๋ชป ํ–ˆ์Œ. ์ด์œ ๋ฅผ ์•Œ์•„๋ณด๋‹ˆ. ๊ณ„์‚ฐ๋œ ๋‚˜๋จธ์ง€ ๋ฐ์ดํ„ฐ์— ์†Œ์ˆ˜ ๊ฐ’์ด ์กด์žฌ. ๊ทธ๋ ‡๊ธฐ์— ๋ฐ์ดํ„ฐ ์ดˆ๊ณผ์ž„์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Œ.

function solution(price) { 
    if(price >= 500000) price *= 0.8;
    else if(price >= 300000) price *= 0.9;
    else if(price >= 100000) price *= 0.95;
    return Math.floor(price )
}

Math ์˜ค๋ธŒ์ ํŠธ์—์„œ floor ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด price์˜ ๋‚˜๋จธ์ง€ ์†Œ์ˆ˜๊ฐ’๋“ค์„ ๋ฒ„๋ ค ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๋ฆฌํ„ด ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ’€์ด๋ฅผ ์™„์„ฑํ•จ.


๐Ÿซข ์ธ์ƒ ๊นŠ์—ˆ๋˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

ํ’€์ด ์ฝ”๋“œ.

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
}

์ด ํ’€์ด์—์„œ ์ธ์ƒ ๊นŠ์—ˆ๋˜ ํ’€์ด ๋ถ€๋ถ„์€ ๋‹ค์ค‘ ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ฐฐ์—ด length๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํ•„์š”ํ•œ length๋ฅผ ๋ฝ‘์•„ ์กฐ๊ฑด์— ์ถฉ์กฑ์‹œํ‚จ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ ๊ฒƒ์ด ์ธ์ƒ๊นŠ๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

0๊ฐœ์˜ ๋Œ“๊ธ€