Skip to main content

Command Palette

Search for a command to run...

[JavaScript] 문자열 다루기 기본 (dealing with strings)

프로그래머스 NaN

Published
•1 min read•View as Markdown
[JavaScript] 문자열 다루기 기본 (dealing with strings)
S

👋 Hi there! I'm Suzie, a developer who began my coding journey in 2023. I’m passionate about building fast and dynamic web experiences using Next.js, JavaScript, and Tailwind CSS.

🌱 As I grow in tech, I enjoy documenting my learning journey and sharing insights to help others navigate their path.

✨ Fun fact: When I’m not coding, you can find me practicing Capoeria 🤸.

Let’s connect, learn, and create together!

문제 설명

문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다.

제한 사항

s는 길이 1 이상, 길이 8 이하인 문자열입니다. s는 영문 알파벳 대소문자 또는 0부터 9까지 숫자로 이루어져 있습니다.

입출력 예

sreturn
"a234"false
"1234"true

와... 친구가 이거 풀고있다고 해서 도전했다가 1시간 순간이동한 느낌.. 처음에 typeof로 접근하면 되겠다! 했는데 string에 *1을하면 알파벳이여도 type이 강제로 number로 변한다는 걸 알게되었다. typeof로는 실패.. includes로 접근했지만 알파벳을 a~z까지 다 써줄수는 없으므로 pass. 배열로 바꿔야겠다! 하고 split을 사용해 배열에 넣어줌. for을 돌려서 typeof가 문자인게 포함되면 false를 return하게 하려고 했으나 숫자들도 type 이 number라서 실패. map을 돌려서 Number로 전체 바꿔줬더니 문자열이였던게 NaN로 출력됨! 오! NaN으로 거르면 되겠다! 하지만 NaN으로 거르기가 쉽지 않았다. MDN문서를 보다가 includes로 sorting이 가능한것을 발견!! includes를 사용해서 문제를 풀었더니 통과!!!! 🥳

Screenshot 2022-10-05 at 10.27.53 PM.png

프로그래머스 9점도 받았다 😆

나의 풀이

function solution(s) {
s = s.split('').map(Number);
    if(s.length === 4 || s.length=== 6){
     if(!s.includes(NaN)){
       return true
       } else{
           return false
       }
    }else {
        return false
    }
}
Z

you can do it like this too:

let solution = (s)=> !!Number(s) && (s.length == 4 || s.length == 6)