λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

ν”„μ—” 곡뢀/🫧 μ•Œκ³ λ¦¬μ¦˜ 곡뢀

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€/Javascript] [level.0] μ™Έκ³„ν–‰μ„±μ˜ λ‚˜μ΄ || μ½”λ”©ν…ŒμŠ€νŠΈ μž…λ¬Έ

728x90

μ•„μŠ€ν‚€ μ½”λ“œλ‘œ " 숫자 -> 문자 " 둜 λ³€ν™˜ν•˜κΈ°

function solution(age) {
    const arr = String(age).split("").map(v => String.fromCharCode(Number(v) + 97))
    return arr.join("");
}

 

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

 

String.fromCharCode() - JavaScript | MDN

String.fromCharCode() λ©”μ„œλ“œλŠ” UTF-16 μ½”λ“œ μœ λ‹›μ˜ μ‹œν€€μŠ€λ‘œλΆ€ν„° λ¬Έμžμ—΄μ„ 생성해 λ°˜ν™˜ν•©λ‹ˆλ‹€.

developer.mozilla.org

 

μ•„μŠ€ν‚€ μ½”λ“œμ—μ„œ a κ°€ 97 μ΄λΌλŠ” 점을 κ°μ•ˆν•΄μ„œ λ°›μ•„μ˜¨ μˆ«μžμ— +97을 λ”ν•œ 숫자λ₯Ό λ³€ν™˜ν•΄μ€¬λ‹€.

 

사싀 μ’€ λ„ˆλ¬΄ μ–΄λ ΅κ²Œ ν‘Ό 감이 μžˆλŠ”λ°, 

겸사겸사 μ•„μŠ€ν‚€ μ½”λ“œμ— λŒ€ν•΄μ„œ μ‚΄νŽ΄λ³Ό κ²Έ μ΄λ ‡κ²Œ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ΄€λ‹€.

 

 

 

μ–΄μ°¨ν”Ό λ²”μœ„κ°€ 0-9 κΉŒμ§€μ˜ 숫자이기 λ•Œλ¬Έμ—, 

function solution(age) {
  return age
    .toString()
    .split("")
    .map((v) => "abcdefghij"[v])
    .join("");
}

a~j κΉŒμ§€ 문자λ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•΄λ„ λ¬΄κ΄€ν•˜λ‹€.

πŸλ¬Έμžμ—΄ μžμ²΄λ„ index λ₯Ό 톡해 char λ₯Ό μΆ”μΆœν•  수 μžˆλ‹€.

 

 

 

728x90
λ°˜μ‘ν˜•