find Nearest square number

samuel Jo·2023년 3월 16일
0

codewars

목록 보기
18/46

description
Your task is to find the nearest square number, nearest_sq(n) or nearestSq(n), of a positive integer n.

For example, if n = 111, then nearest_sq(n) (nearestSq(n)) equals 121, since 111 is closer to 121, the square of 11, than 100, the square of 10.

If the n is already the perfect square (e.g. n = 144, n = 81, etc.), you need to just return n.

codewars 8kyu문제

function nearestSq(n){
 return Math.pow(Math.round(Math.sqrt(n)), 2);

}

Math.pow(x,y) = x^y
Math.round(x) =x랑 가장 가까운정수.
Math.sqrt(n)= √n

round메서드를 통해 매개변수와 가장 가까운 정수를 만들어주고, 그 정수에 루트를 씌운다.
루트n^2은 결국 n을 반환한다.

여담) Count the Islands 문제풀다가 리프레시로...

profile
step by step

0개의 댓글