๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript] ์˜น์•Œ์ด(1) || ์ฝ”๋”ฉํ…Œ์ŠคํŠธ + ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 0๋‹จ๊ณ„ ์™„์„ฑ

728x90

 

function solution(babbling) {
    const words = ["aya", "ye", "woo", "ma"];
    let count = 0;
    for (let i = 0; i < babbling.length; i++) {
        words.map(word => {
            babbling[i] = babbling[i].replace(String(word), " ");
        })
    }
    babbling.map(v => v.replaceAll(" ", "") === "" && count++)
    return count;
}

 

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

๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋ฅผ words ์— ๋„ฃ๊ณ  

์˜ˆ์‹œ์˜ ๋‹จ์–ด๋“ค์ด ์ด ๋‹จ์–ด๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์‹œ replace ๋ฅผ ํ†ตํ•ด ์†Œ๊ฑฐํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๊ณต๋ฐฑ์ธ "" ๋กœ replaceAll ์„ ํ•˜๋ฉด, "wyeoo" ๊ฐ™์€ ์ผ€์ด์Šค๋„ ํ†ต๊ณผ๋˜๊ธฐ ๋•Œ๋ฌธ์— " " ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜๋‚˜ ์ค€ ์ƒํƒœ๋กœ replace ๋ฅผ ํ•ด์คฌ๋‹ค.

๊ทธ๋Ÿผ "wyeoo" => "w oo" ์ด๋ ‡๊ฒŒ ๋ฐ”๋€Œ๋ฏ€๋กœ, ์˜น์•Œ์ด ์š”์†Œ ์ค‘ ํ•˜๋‚˜์ธ "woo" ๋กœ ๋Œ€์ฒด๋˜์ง€ ์•Š๋Š”๋‹ค. 

๋ฌธ์ œ์— ํ•˜๋‚˜์˜ ์˜น์•Œ์ด๋Š” ์ตœ๋Œ€ ํ•˜๋‚˜๋งŒ ํฌํ•จ๋œ๋‹ค ํ•˜์—ฌ replaceAll ๋Œ€์‹  replace ๋งŒ ์จ์ค˜๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

[ '  ', 'uuu ', ' ', '   ', ' a' ]

for๋ฌธ์„ ๋‹ค ๋Œ๋ฉด ์ด๋Ÿฐ์‹์œผ๋กœ ๋˜๊ธฐ ๋•Œ๋ฌธ์—

๋„์–ด์“ฐ๊ธฐ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์š”์†Œ๋“ค์„ ๊ณต๋ฐฑ์œผ๋กœ ์ „๋ถ€ ์ฒ˜๋ฆฌํ•ด์ค€ ํ›„, ๊ณต๋ฐฑ์ธ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

โ˜˜๏ธ ์ •๊ทœ์‹ ํ™œ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

function solution(babbling) {
  var answer = 0;
  const regex = /^(aya|ye|woo|ma)+$/;

  babbling.forEach(word => {
    if (regex.test(word)) answer++;  
  })

  return answer;
}

์ •๊ทœ์‹์„ ์ž˜ ์•Œ๋ฉด ๋” ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

๊ธฐํ˜ธ์˜ ์˜๋ฏธ๋“ค์ด ์•„์ง ์ข€ ๋‚ฏ์„ค์ง€๋งŒ ์ด๋Ÿด ๋•Œ ํ•˜๋‚˜์”ฉ ์ตํ˜€๋‘๋ฉด ์ข‹์„ ๋“ฏ ํ•˜๋‹ค.

 

^ : ๋ฌธ์ž์—ด ๋˜๋Š” ์ค„์˜ ์‹œ์ž‘์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ^0์€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.
$: ๋ฌธ์ž์—ด ๋˜๋Š” ์ค„์˜ ๋์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0$๋Š” 0์œผ๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

โ˜˜๏ธ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 0 ๋‹จ๊ณ„ ํ†ต๊ณผ!

 

๊ทธ๋ฆฌ๊ณ  ๋“œ๋””์–ด 0๋‹จ๊ณ„ 100๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ’€์—ˆ๋‹ค!!!

์ฒ˜์Œ์—” ์—„์ฒญ ๋งŽ์•„๋ณด์˜€๋Š”๋ฐ, ์–ด๋Š์ƒˆ ํ•œํŽ˜์ด์ง€๋งŒ ๋‚จ๊ณ , ๋˜ ์–ด๋Š์ƒˆ ๋ช‡ ๋ฌธ์ œ๋งŒ ๋‚จ๋”๋‹ˆ ๋‹ค ํ’€์—ˆ๋‹ค! ์•ผํ˜ธ!

๋งค์ผ 1-2์‹œ๊ฐ„ ์ด๋‚ด๋กœ ํ’€์—ˆ๊ณ  ์•ฝ 3๊ฐœ์›” ์ •๋„ ๊ฑธ๋ ธ๋‹ค. ๋ฐฑ์ค€ ๋ฌธ์ œ๋„ ํ•จ๊ป˜ ํ’€๋‹ค๋ณด๋‹ˆ ๊ธฐ๊ฐ„์ด ๋” ๊ธธ์–ด์ง„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

ํ‚ค์•ผ ๋ฟŒ๋“ฏ~~~

๋‚œ ์ฒœ์žฌ์•ผ์™€ ๋‚œ ๋ฐ”๋ณด์•ผ๋ฅผ ์ˆ˜๋„ ์—†์ด ์™”๋‹ค๊ฐ”๋‹ค ํ•œ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ‘œ๋กœ ๋ณด๋‹ˆ ๋งŒ์กฑ์Šค๋Ÿฝ๋‹ค.

์•ž์œผ๋กœ๋„ ์กฐ๊ธˆ ๋” ๋ˆ๊ธฐ ์žˆ๊ฒŒ ๋„์ „ํ•˜๊ณ , ์—ฌ๋Ÿฌ๋ฐฉ๋ฉด์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ ค๊ณ  ๋…ธ๋ ฅํ•ด์•ผ์ง€.

 

 

728x90
๋ฐ˜์‘ํ˜•