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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

Eun_zii 2024. 2. 5. 17:28

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

- ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

 

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

  • ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด๋ž€ ๋ฌธ์ž์—ด์—์„œ ์—ฐ์†๋œ ์ผ๋ถ€๋ถ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด "ana", "ban", "anana", "banana", "n"๋Š” ๋ชจ๋‘ ๋ฌธ์ž์—ด "banana"์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด์ง€๋งŒ, "aaa", "bnana", "wxyz"๋Š” ๋ชจ๋‘ "banana"์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์•„๋‹™๋‹ˆ๋‹ค.        ๋ฌธ์ž์—ด my_string๊ณผ target์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, target์ด ๋ฌธ์ž์—ด my_string์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด 1์„, ์•„๋‹ˆ๋ผ๋ฉด 0์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

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

  • 1 ≤ my_string์˜ ๊ธธ์ด ≤ 100
  • my_string์€ ์˜์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 1 ≤ target์˜ ๊ธธ์ด ≤ 100
  • target์€ ์˜์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’š ํ’€์ด

function solution(my_string, target) {
    if(my_string.includes(target)) {
        return 1
    } else {
        return 0
    }
}

 

โœ… 

๐Ÿ’กincludes() ๋ฉ”์„œ๋“œ๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์ด ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํŒ๋ณ„ํ•˜๊ณ , ๊ฒฐ๊ณผ๋ฅผ true ๋˜๋Š” false ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ์‹œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ž์—ด "my_string" ์— target์ด ์žˆ๋Š”์ง€ includes()๋กœ ํ™•์ธํ•˜๊ณ  target์ด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด๋ฉด 1 ์•„๋‹ˆ๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ–ˆ๋‹ค.

728x90