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

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript/์ฝ”๋”ฉ๊ธฐ์ดˆํŠธ๋ ˆ์ด๋‹] ์ ‘๋ฏธ์‚ฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

728x90
function solution(my_string, is_suffix) {
  return my_string.slice(- is_suffix.length) === is_suffix ? 1 : 0;
}

์ด๋ ‡๊ฒŒ ์ฃผ์–ด์ง„ ๋ฌธ์ž์˜ length ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ, 

string.endsWith() ๋ผ๋Š”๊ฒŒ ์žˆ์—ˆ๋‹ค. 

 

String.prototype.endsWith()

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

 

String.prototype.endsWith() - JavaScript | MDN

The endsWith() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ true ํ˜น์€ false๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

developer.mozilla.org

 

 

The endsWith() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ true ํ˜น์€ false๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๋ฌธ๋ฒ•

str.endsWith(searchString[, length])

ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค

searchString

์ด ๋ฌธ์ž์—ด์˜ ๋์ด ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ์ฐพ๊ธฐ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

length

์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ’์ด๋ฉฐ, ๊ธฐ๋ณธ๊ฐ’์€ ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ’์€ ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด ์•ˆ์—์„œ๋งŒ ์กด์žฌํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

const solution = (str, suff) => str.endsWith(suff) ? 1 : 0

 

์ด๋ ‡๊ฒŒ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

728x90
๋ฐ˜์‘ํ˜•