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

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

Study/Coding Test

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

Jieunny 2023. 2. 1. 14:56

๐Ÿ“Œ  ๋ฌธ์ œ

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


์ œํ•œ์‚ฌํ•ญ

  • 1 ≤ babbling์˜ ๊ธธ์ด ≤ 100
  • 1 ≤ babbling[i]์˜ ๊ธธ์ด ≤ 30
  • ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’ก  ์•„์ด๋””์–ด

babbling ๋Œ๋ฉด์„œ ๋งํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด ๋‚˜์˜ค๋ฉด '0'์œผ๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

์น˜ํ™˜ํ•  ๋•Œ replace ๋ง๊ณ  replaceAll์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

(replace ์จ์„œ ayameaya ์ฒ˜๋Ÿผ ์—ฐ์†์œผ๋กœ ๋ฐ˜๋ณต๋˜์ง€ ์•Š์•„๋„ aya๋ฅผ ํ•˜๋‚˜๋งŒ ์น˜ํ™˜ํ•ด์„œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๊ฑธ๋ ค์„œ ๊ณ ์ƒํ–ˆ์Œ..)

์ด๋•Œ, ๋ฌธ์ž์—ด์„ 2๋ฒˆ ๋ฐ˜๋ณตํ•œ ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด ๋ฐœ์Œํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ break๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

Number(๋ฌธ์ž์—ด) ํ–ˆ์„ ๋•Œ 0์ด ๋‚˜์˜ค๋ฉด ๋ชจ๋‘ ์น˜ํ™˜๋œ ๊ฒƒ์ด๋ฏ€๋กœ answer++ ํ•ด์ค€๋‹ค.

 

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

function solution(babbling) {
  var answer = 0;
  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].repeat(2))){;
        babbling[i] = babbling[i].replace(canSpeak[j], '!');
      }
      if(babbling[i].includes(canSpeak[j])){
        babbling[i] = babbling[i].replaceAll(canSpeak[j], '0');
      }
    }
  }
  for(let i=0; i<babbling.length; i++){
    if(Number(babbling[i]) === 0){
      answer++;
    }
  }

  return answer;
}