๐ŸŸฃ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํšŒ๊ณ 

๊น€ํƒœ์šฑยท2023๋…„ 3์›” 28์ผ
0
post-thumbnail

๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #1

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv1: ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
๋ฌธ์ž์—ด s์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
s๋Š” ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ
str์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

sreturn
"Zbcdefg""gfedcbZ"

๋‚˜์˜ ํ’€์ด

function solution(s) {
  var answer = s
    .split("")
    .sort((a, b) => a - b)
    .reverse()
    .join("");
  return answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด split ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
2. sort ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ ์ธ์ž๋ฅผ ๋น„๊ต ํ›„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‚˜์—ด
3.์œ„ ์กฐ๊ฑด์—์„œ ๋Œ€๋ฌธ์ž๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด reverse ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์—ญ์ˆœ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๋‹ค.

๋ฌธ์ œ์ 
1. ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ์กฐ๊ฑด์˜ ์ž…์ถœ๋ ฅ์€ ํ†ต๊ณผ๊ฐ€ ๋˜์ง€๋งŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ํ†ต๊ณผ๊ฐ€ ๋˜์ง€ ๋ชป ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊น€.
2. ๋ฌธ์ž์—ด์—์„œ sort๋Š” ascii ์ฝ”๋“œ์˜ ์œ ๋‹ˆ์ฝ”๋“œ ํ‘œ๊ธฐ ๊ฐ’์œผ๋กœ ๋น„๊ต๊ฐ€ ์ด๋ค„์ง€๋Š”๋ฐ
sort ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ a - b ์—ฐ์‚ฐ์€ ์ˆซ์ž ํ˜•๋งŒ ๊ฐ€๋Šฅ.
a-b๋กœ ์—ฐ์‚ฐ ๋œ ๊ฐ’์ด 0์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘๊ฑฐ๋‚˜ ์ปค์•ผ ์ฐจ์ˆœ ์ •๋ ฌ์ด ์ด๋ค„์ง„๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ž์—ด์€ ์—ฐ์‚ฐ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋ฉฐ, ๋ฌธ์ž์—ด์€ ์ธ์ž์˜ (a-b)์˜ ์—ฐ์‚ฐ์œผ๋กœ ์ฐธ ๊ฑฐ์ง“์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ๋น„๊ต ์„ฑ๋ฆฝ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€.

์œ ๋‹ˆ์ฝ”๋“œ์˜ ์ˆซ์ž ํ‘œ๊ธฐ ํฌ๊ธฐ๋ฅผ ํ†ตํ•ด ๋น„๊ต๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ต์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, localeCompare( )๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋Œ€์†Œ ๋น„๊ต๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ ์ด์œ ๋Š” ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.
sort ๊ธฐ๋Šฅ ์˜ˆ์‹œ)

๊ทธ๋ ‡๊ธฐ์— ๋ฌธ์ž์—ด ์ž์ฒด๋ฅผ ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์œผ๋ฉด 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•œ๋‹ค.

ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•œ ๊ฐ’์˜ ์กฐ๊ฑด์„ ํ†ตํ•ด -1, 0, 1 ๊ฐ’์ค‘ ํ•˜๋‚˜๋กœ ๋ฐ˜ํ™˜ ํ›„ ์ •๋ ฌ.

  1. ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    sort((a, b) => {
        return a < b ? -1 : a > b ? 1 : 0;
      })
  2. ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•œ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    sort((a, b) => {
        return a > b ? -1 : a < b ? 1 : 0;
      })
                             

String.prototype.localeCompare() ๋ฉ”์†Œ๋“œ ์ด์šฉ

localeCompare() ๋ฉ”์„œ๋“œ๋Š” ์ฐธ์กฐ ๋ฌธ์ž์—ด์ด ์ •๋ ฌ ์ˆœ์œผ๋กœ ์ง€์ •๋œ ๋ฌธ์ž์—ด ์•ž ํ˜น์€ ๋’ค์— ์˜ค๋Š”์ง€ ๋˜๋Š” ๋™์ผํ•œ ๋ฌธ์ž์—ด์ธ์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  1. localeCompare( ) ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    sort((a, b) => a.localeCompare(b))
  2. localeCompare( ) ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    sort((a, b) => b.localeCompare(a))

๊ทธ๋Ÿฌ๋‚˜ ์œ„์˜ ๋‘ ์‹์—๋„ ์ฐจ์ด๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž๋ฉด

var str = "Zbcdefg";
var split = str.split("");

console.log(split.sort((a, b) => {
      return a < b ? -1 : a > b ? 1 : 0;
    }).join(""));

console.log(split.sort((a, b) => {
      return a > b ? -1 : a < b ? 1 : 0;
    }).join(""));

console.log(split.sort((a, b) => a.localeCompare(b)).join(""));
console.log(split.sort((a, b) => b.localeCompare(a)).join(""));

// ์•„์Šคํ‚ค ์ฝ”๋“œ ์ˆซ์ž์˜ ๋น„๊ต ๊ฐ’์— ๋”ฐ๋ผ ์ฐจ์ˆœ ์ •๋ ฌ ์ ์šฉ.
Zbcdefg  
gfedcbZ

// ๋ฌธ์ž ์‚ฌ์ „ ์ˆœ์„œ ๊ทธ๋Œ€๋กœ ์ฐจ์ˆœ ์ •๋ ฌ ์ ์šฉ.
bcdefgZ
Zgfedcb

sort() ๋ฉ”์†Œ๋“œ ๊ธฐ๋Šฅ ์ž์ฒด์— ๋‹ค์‹œ๊ธˆ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ณ„๊ธฐ๊ฐ€ ๋˜์—ˆ๊ณ  gpt ๋„์›€๊ณผ ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด์„œ ๋ฌด์—‡์ด ๋ฌธ์ œ์ธ์ง€ sort ๋ฉ”์†Œ๋“œ์— ์ข€ ๋” ๋‹ค๊ฐ€๊ฐˆ ์ˆ˜์žˆ์—ˆ๋‹ค.

map์„ ํ†ตํ•ด sort ๋ฉ”์†Œ๋“œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋Š”๋ฐ ์ฐจํ›„ ์ดํ•ดํ•˜๋„๋ก ํ•ด๋ณด์Ÿˆ,,


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(s) {
return s
  .split("")
  .sort()
  .reverse()
  .join("");
}

ํ’€์ด
1. ๋ฌธ์ž์—ด sortํ•  ๋•Œ๋Š” localeCompare์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜
2. sort([compareFunction]) ์ธ์ž ๊ฐ’์œผ๋กœ ์•„๋ฌด๊ฒƒ๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ์–ป๊ณ ์ž ํ•˜๋Š” ์ •๋ ฌ์ด ๋  ๊ฒƒ์ด๋‹ค,,!

#2

  function solution(s) {
    return s.split("").sort((a,b) => a<b ? 1:-1).join("")
}

ํ’€์ด

  1. ๋น„๊ต ์กฐ๊ฑด์„ ํ†ตํ•ด ์–ป์€ ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์…จ๋‹ค..!

๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #2

์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ๋ง์…ˆ

๋ฌธ์ œ ์„ค๋ช…
๋‘ ์ •์ˆ˜ left์™€ right๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. left๋ถ€ํ„ฐ right๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜๋“ค ์ค‘์—์„œ, ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ธ ์ˆ˜๋Š” ๋”ํ•˜๊ณ , ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ์ˆ˜๋Š” ๋บ€ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
1 โ‰ค left โ‰ค right โ‰ค 1,000

์ž…์ถœ๋ ฅ ์˜ˆ

leftrightresult
131743
242752

๋‚˜์˜ ํ’€์ด

  function collect(num) {
  let sum = 0;
  for (let i = 1; i <= num; i++) {
    if (num % i === 0) sum += 1;
  }
  return sum;
}

function solution(left, right) {
  let answer = 0;
  for (let i = left; i <= right; i++) {
    collect(i) % 2 === 0 ? (answer += i) : (answer -= i);
  }
  return answer;
}

๊ตฌํ˜„ ์ ‘๊ทผ
1. collect ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ธ์ž์— ๋Œ€์ž…๋˜๋Š” ๊ฐ’์˜ ์•ฝ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ๊ตฌํ•˜๊ณ 
2. solution ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด left์—์„œ right์˜ ๋ฒ”์œ„๊นŒ์ง€ ์ˆซ์ž๋“ค์˜ ์•ฝ์ˆ˜๊ฐ€ ์ง์ˆ˜ ๊ฐ’์ด๋ฉด ๋”ํ•˜๊ณ  ํ™€์ˆ˜ ๊ฐ’์ด๋ฉด ๋นผ๋Š” ํ•จ์ˆ˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„.

๋ฌธ์ œ์ 
ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋งŒ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ คํ•˜์ง€ ๋ง๊ณ  ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ป๊ณ ์ž ํ•˜๋Š” ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ž.
์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์•ฝ์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๋Š” ํ•จ์ˆ˜์™€
ํŒŒ์•…ํ•œ ๊ฐœ์ˆ˜๋ฅผ ํ™€, ์ง ๊ตฌ๋ณ„ํ•˜์—ฌ ํ•ฉ์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ–ˆ๋‹ค.


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function solution(left, right) {
let answer = 0;

for (let i = left; i <= right; i++) {
  let count = 0;
  for (let j = 1; j <= i; j++) {
    if (i % j === 0) count++; // ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜ ๋ˆ„์‚ฐ
  }
  if (count % 2)
    answer -= i; // ์ „์ฒด ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ์กด์žฌํ•˜๋ฉด ๋บ€ ๊ฐ’์„ ํ• ๋‹น
  else answer += i; // ์ „์ฒด ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋”ํ•œ ๊ฐ’์„ ํ• ๋‹น
}

return answer;
}

ํ’€์ด
์ด์ค‘ for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ ํ•ด๋ณด๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ๊ตฌํ˜„ ํ•˜์‹  ๋ถ„์ด ๊ณ„์…”์„œ ๊ฐ€์ ธ์™€๋ดค๋‹ค.
1. answer์˜ for ๋ฌธ์œผ๋กœ count์˜ ๊ฐ’์„ ๊ตฌํ•ด ํ™€,์ง ๊ตฌ๋ณ„ ํ›„ ์กฐ๊ฑด ํ•ฉ์‚ฐ
2. count์˜ for ๋ฌธ์œผ๋กœ ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ๋ˆ„์‚ฐ.

#2

function solution(left, right) {
  var answer = 0;
  for (let i = left; i <= right; i++) {
      if (Number.isInteger(Math.sqrt(i))) {
          answer -= i;
      } else {
          answer += i;
      }
  }
  return answer;
}

ํ’€์ด
1. ์ œ๊ณฑ๊ทผ์ด ์ •์ˆ˜๋ฉด ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋‹ค...


๐Ÿ˜ƒ ์˜ค๋Š˜์˜ ๋ฌธ์ œ ํ•ฅ์ง #3

๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

๋ฌธ์ œ ์„ค๋ช…
๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 4 ํ˜น์€ 6์ด๊ณ , ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด s๊ฐ€ "a234"์ด๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  "1234"๋ผ๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • s๋Š” ๊ธธ์ด 1 ์ด์ƒ, ๊ธธ์ด 8 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์˜๋ฌธ ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋˜๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

sreturn
"a234"false
"1234"true

๋‚˜์˜ ํ’€์ด

function solution(s) {
var answer = s
  .split("")
  .map((e) => e * 1)
  .every((cur) => typeof cur === "number");

return answer;
}

console.log(solution("a234"));

๊ตฌํ˜„ ์ ‘๊ทผ
1. split ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ์ค€ ๋‹ค์Œ
2. map ๋ฉ”์†Œ๋“ค ํ†ตํ•ด ๋ฐฐ์—ด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ๊ฐ *1 ์—ฐ์‚ฐํ•˜์—ฌ ์ˆซ์žํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜์„ ํ•ด์ฃผ์—ˆ๋‹ค.
3. ๋งˆ์ง€๋ง‰์œผ๋กœ every ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ ์กฐ๊ฑด์ด ํ†ต๊ณผ๊ฐ€ ๋˜๋Š”์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„

๋ฌธ์ œ์ 
1. typeof์˜ ๋ฌธ์ œ

cur = ['a','2','3','4'] ์ผ ๋•Œ
.every((cur) => typeof cur === Number);
// true

์ง€๊ธˆ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์€ type of์—์„œ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๊ฐœ์„  ํ›„ every ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ typeof์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด 'number'๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋ฅผ ๋ฐ”๋žฌ๋Š”๋ฐ,,

cur = ['a','2','3','4']์ผ ๋•Œ๋„ true๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ ์ด์—ˆ๋‹ค.

console.log(cur)์„ ํ•ด๋ณด๋‹ˆ NaN์ด ๋‚˜์˜ค๋Š” ๊ฒƒ.
NaN์€ typeof ํ•˜๋ฉด number๋กœ ์ทจ๊ธ‰.

๋ฌธ์ œ ๊ฐœ์„ 
1. cur์˜ ์ธ์ž ๊ฐ’์ด ์ˆซ์ž๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ isNaN ๋ฉ”์†Œ๋“œ๋ฅผ
2. every ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ˆœํ™˜ํ•˜์—ฌ ์ฐธ ๊ฑฐ์ง“ ํŒ๋ณ„.


๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

#1

function alpha_string46(s) {
 return s.length == 4 || s.length == 6 ? !isNaN(s) : false 
}

ํ’€์ด
s์˜ ์ธ์ž๋กœ ์ „๋‹ฌ ๋˜๋Š” ๋ฌธ์ž์—ด์ด ์ˆซ์žํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•œ์ง€ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ํ•œ๋ฒˆ์— ์—ฌ๋ถ€ ํŒŒ์•….

๊ทธ๋Ÿฌ๋‚˜ ์ง€๊ธˆ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์œผ๋กœ๋Š” ์ธ์ž ๊ฐ’์ด ์ง€์ˆ˜๋กœ ์˜ค๊ฒŒ๋˜๋ฉด ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชป ํ•œ๋‹ค.

s = "1e22"

๋‚˜์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ split์œผ๋กœ ํ•˜๋‚˜์”ฉ ์ชผ๊ฐœ์–ด ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

#2

function alpha_string46(s){
  var regex = /^\d{6}$|^\d{4}$/;

return regex.test(s);
}

ํ’€์ด
์ •๊ทœ ํ‘œํ˜„์‹ ๋ฉ”์†Œ๋“œ test๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ ๋ฐฉ์‹์ด๋‹ค.
์ •๊ทœ ํ‘œํ˜„์‹์€ ์•„์ง ์ž˜ ๋‹ค๋ฃฐ ์ค„ ๋ชฐ๋ผ ์ ‘๊ทผํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค..

profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

0๊ฐœ์˜ ๋Œ“๊ธ€