프로그래머스 1단계 - 정수 제곱근 판별

원동휘·2022년 9월 19일
0

프로그래머스

목록 보기
4/46

풀이

  • js 내장 메소드를 이용해 풀이
    - 먼저 Math.sqrt를 이용해 제곱근을 구하고, 해당제곱근이 정수 제곱근이 맞는지 확인하기위해
    Math.floor한 값과 비교해 같다면 정수제곱근, 다르다면 정수제곱근이 아닌걸로 판단.
    true면 Math.pow를 이용한 제곱 false면 -1로 풀이
function solution(n) {
  const sqrt = Math.sqrt(n)
  return sqrt === Math.floor(sqrt) ? Math.pow(sqrt + 1 , 2) : -1
}

console.log(solution(121))
console.log(solution(3))

profile
Front-End Developer #Nextjs #React #Typescript

0개의 댓글