์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜1] ๊ฐ€์šด๋ฐ ๋ฌธ์ž ์ถœ๋ ฅ (Javascript)

์€์ง„ 2021. 6. 25. 10:00

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜1] ๊ฐ€์šด๋ฐ ๋ฌธ์ž ์ถœ๋ ฅ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธIdea Sketch

2021-06-25

1. str.substring(begin, end) : ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ง์ „๊นŒ์ง€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜
const str = 'Mozilla';

console.log(str.substring(1, 3));  // output: "oz"

console.log(str.substring(2));  // output: "zilla"
2. str.substr(begin, length) : ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž ๊ธธ์ด ๋งŒํผ์˜ ๋ฌธ์ž๋“ค์„ ๋ฐ˜ํ™˜
const str = 'Mozilla';

console.log(str.substr(1, 2));  // output: "oz"

console.log(str.substr(2));  // output: "zilla"
3. ๋ฌธ์ž์—ด์ด ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ
return str.substr(Math.floor(arr.length/2), 1);
4. ๋ฌธ์ž์—ด์ด ์ง์ˆ˜์ธ ๊ฒฝ์šฐ
let mid = Math.floor(arr.length);
return str.substring(mid-1, mid+1);

 

โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-25

function solution(str) {
  let n = str.length;
  if (n%2) return str.substr(Math.floor(n/2), 1);  // ํ™€์ˆ˜
  else {
    let mid = Math.floor(n/2);
    return str.substring(mid-1, mid+1);  // ์ง์ˆ˜
  }
}