반응형
Number.isInteger() 메서드는 주어진 값이 정수인지 판별합니다.
function fits(x, y) {
if (Number.isInteger(y / x)) {
return 'Fits!';
}
return 'Does NOT fit!';
}
console.log(fits(5, 10));
// Expected output: "Fits!"
console.log(fits(5, 11));
// Expected output: "Does NOT fit!"
Math.sqrt() 함수는 숫자의 제곱근을 반환합니다.
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
연습
function solution1(num) {
return Number.isInteger(Math.sqrt(num)) ? 1 : 2;
}
function solution2(num) {
return Math.sqrt(num) % 1 == 0 ? 1 : 2;
}
function solution3(num) {
return Math.pow(num, 0.5) % 1 == 0 ? 1 : 2;
}
let num = [144, 9, 352];
for (let i = 0; i < num.length; i++) {
console.log('solution1', solution1(num[i]));
console.log('solution2', solution2(num[i]));
console.log('solution3', solution3(num[i]));
}
결과
반응형
댓글