반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

undefined

[프로그래머스] 정수 제곱근 판별 -JS 본문

Coding Test

[프로그래머스] 정수 제곱근 판별 -JS

JavaScripter 2022. 5. 15. 01:45
반응형

문제 설명


문제 풀이

function solution(n) {
    let testNum = Math.pow(n,0.5)
    return Number.isInteger(testNum) ? Math.pow(testNum+1,2) : -1
}

1. Math.pow(n,0.5) = n의 0.5 제곱를 하여 n의 제곱근 추출

 

2. Number.isInteger(testNum)으로 testNum이 정수인지 판별

 

3. 정수 라면 testNum + 1의 제곱 리턴 / 소수 라면 -1 리턴 


개선 사항

Math.pow(n,0.5) = Math.sqrt(n)  // Math.sqrt(num) => num의 제곱근을 구함

반응형
Comments