[Java Script] "정수를 내림차순으로 배치하기"

soor.dev·2021년 2월 17일
0

Coding test

목록 보기
7/18
post-thumbnail

1. 문제 설명

함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.

2. 제한 조건

n은 1이상 8000000000 이하인 자연수입니다.

3. 입출력 예

4. 문제 해결

function solution(n) {
    const x = n.toString().split("");
    const y = x.sort((a, b) => b - a);
    const result = x.join("");
    return Number(result);
}

5. 배운 내용

Number() // string -> number
Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25.
The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.

Number('123')  // returns the number 123
Number('123') === 123  // true

0개의 댓글

관련 채용 정보