Jieunny์˜ ๋ธ”๋กœ๊ทธ

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์˜น์•Œ์ด(1) ๋ณธ๋ฌธ

Study/Coding Test

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์˜น์•Œ์ด(1)

Jieunny 2023. 1. 10. 15:04

๐Ÿ“Œ  ๋ฌธ์ œ

๋จธ์“ฑ์ด๋Š” ํƒœ์–ด๋‚œ ์ง€ 6๊ฐœ์›” ๋œ ์กฐ์นด๋ฅผ ๋Œ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ์นด๋Š” ์•„์ง "aya", "ye", "woo", "ma" ๋„ค ๊ฐ€์ง€ ๋ฐœ์Œ์„ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ ์‚ฌ์šฉํ•ด ์กฐํ•ฉํ•œ(์ด์–ด ๋ถ™์ธ) ๋ฐœ์Œ๋ฐ–์— ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฐฐ์—ด babbling์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,  ๋จธ์“ฑ์ด์˜ ์กฐ์นด๊ฐ€ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

โœ๏ธ ํ’€์ด

function solution(babbling) {
    var answer = 0;
    let alphabet = /[a-z]/;	// ์†Œ๋ฌธ์ž๋ฅผ ์ •๊ทœ์‹์œผ๋กœ ๋‹ด์•„๋†“๋Š”๋‹ค.
    let canSpeak = ['aya', 'ye', 'woo', 'ma'];
    for(let i=0; i<babbling.length; i++){  
        for(let j=0; j<canSpeak.length; j++){
            if(babbling[i].includes(canSpeak[j])) {	// ๋งํ•ง ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉด !๋กœ ์น˜ํ™˜
                babbling[i] = babbling[i].replace(canSpeak[j], '!');
            } 
        }
    }
   for(let i=0; i<babbling.length; i++){	// !๋ง๊ณ  ์†Œ๋ฌธ์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋งํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ false์ผ ๋•Œ ์นด์šดํŠธ ํ•ด์ค€๋‹ค
        if(alphabet.test(babbling[i]) === false){
            answer++;
        };
   }    
    
    return answer;
}

โžฐ replace() ์“ฐ๋ฉด ๋ณ€์ˆ˜์— ๋‹ด์•„์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ ์ž์ฒด๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—!!

โžฐ ๊ณต๋ฐฑ์œผ๋กœ replace ํ•˜๊ฒŒ ๋˜๋ฉด wyeoo ์˜ ๊ฒฝ์šฐ ye๊ฐ€ ์—†์–ด์ง€๋ฉด woo๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์ด๊ฒŒ 0๋‹จ๊ณ„..?