๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ปคํ”ผ ์‹ฌ๋ถ€๋ฆ„ ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ปคํ”ผ ์‹ฌ๋ถ€๋ฆ„

Eun_zii 2024. 3. 28. 17:47

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

- ์ปคํ”ผ ์‹ฌ๋ถ€๋ฆ„

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ํŒ€์˜ ๋ง‰๋‚ด์ธ ์ฒ ์ˆ˜๋Š” ์•„๋ฉ”๋ฆฌ์นด๋…ธ์™€ ์นดํŽ˜ ๋ผํ…Œ๋งŒ ํŒ๋งคํ•˜๋Š” ์นดํŽ˜์—์„œ ํŒ€์›๋“ค์˜ ์ปคํ”ผ๋ฅผ ์‚ฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ฉ”๋ฆฌ์นด๋…ธ์™€ ์นดํŽ˜ ๋ผํ…Œ์˜ ๊ฐ€๊ฒฉ์€ ์ฐจ๊ฐ€์šด ๊ฒƒ๊ณผ ๋œจ๊ฑฐ์šด ๊ฒƒ ์ƒ๊ด€์—†์ด ๊ฐ๊ฐ 4500, 5000์›์ž…๋‹ˆ๋‹ค. ๊ฐ ํŒ€์›์—๊ฒŒ ๋งˆ์‹ค ๋ฉ”๋‰ด๋ฅผ ์ ์–ด๋‹ฌ๋ผ๊ณ  ํ•˜์˜€๊ณ , ๊ทธ ์ค‘์—์„œ ๋ฉ”๋‰ด๋งŒ ์ ์€ ํŒ€์›์˜ ๊ฒƒ์€ ์ฐจ๊ฐ€์šด ๊ฒƒ์œผ๋กœ ํ†ต์ผํ•˜๊ณ  "์•„๋ฌด๊ฑฐ๋‚˜"๋ฅผ ์ ์€ ํŒ€์›์˜ ๊ฒƒ์€ ์ฐจ๊ฐ€์šด ์•„๋ฉ”๋ฆฌ์นด๋…ธ๋กœ ํ†ต์ผํ•˜๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ ์ง์›์ด ์ ์€ ๋ฉ”๋‰ด๊ฐ€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด order๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์นดํŽ˜์—์„œ ๊ฒฐ์ œํ•˜๊ฒŒ ๋  ๊ธˆ์•ก์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. order์˜ ์›์†Œ๋Š” ์•„๋ž˜์˜ ๊ฒƒ๋“ค๋งŒ ๋“ค์–ด์˜ค๊ณ , ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ order์˜ ๊ธธ์ด ≤ 1,000

๐Ÿ’š ํ’€์ด

function solution(order) {
    let answer = 0;
    const americanoPrice = 4500;
    const cafelattePrice = 5000;
    for ( let i = 0; i < order.length; i++ ) {
        if( order[i].includes("americano")) {
            answer += americanoPrice
        } else if ( order[i].includes("cafelatte")) {
            answer += cafelattePrice
        } else if ( order[i] === "anything") {
            answer += americanoPrice
        }
    }
    return answer
}

 

โœ… 

order์˜ ๊ธˆ์•ก์„ ํ•ฉํ•œ ๊ธˆ์•ก์„ ๋‹ด์„ answer ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด๋‘๊ณ ,

์•„๋ฉ”๋ฆฌ์นด๋…ธ์™€ ์นดํŽ˜๋ผํ…Œ ๊ธˆ์•ก์„ ์„ ์–ธํ•ด๋‘”๋‹ค.

for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ order์˜ ๋ฉ”๋‰ด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ 

order[i] ์— americano ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด ์•„๋ฉ”๋ฆฌ์นด๋…ธ ๊ธˆ์•ก์„ answer์— ๋”ํ•ด์ฃผ๊ณ ,

cafelatte๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด ์นดํŽ˜๋ผํ…Œ ๊ธˆ์•ก์„ answer์— ๋”ํ•ด์ค€๋‹ค. anything์€ ์•„์ด์Šค์•„๋ฉ”๋ฆฌ์นด๋…ธ๋กœ ํ†ต์ผํ•˜๊ธฐ๋กœ ํ–ˆ๊ธฐ๋•Œ๋ฌธ์—

anything์ด ์žˆ์œผ๋ฉด ์•„๋ฉ”๋ฆฌ์นด๋…ธ ๊ธˆ์•ก์„ ๋”ํ•ด์ค€๋‹ค. ๊ทธํ›„ return ํ•ด์ฃผ๋ฉด ๋œ๋‹ค !

728x90