반응형
1. 비트 AND 연산자 사용
function solution1(n) {
return (n & (n - 1)) === 0;
}
const n = [8, 16, 27, 32, 81];
for (let i = 0; i < n.length; i++) {
console.log('solution1', solution1(n[i]));
}
결과
2. log 사용
function solution2(n) {
const answer = Math.log2(n);
return Number.isInteger(answer);
}
function solution3(n) {
const answer = Math.log10(n) / Math.log10(3);
return Number.isInteger(answer);
}
const n = [8, 16, 27, 32, 81];
for (let i = 0; i < n.length; i++) {
console.log('solution2', solution2(n[i]));
console.log('solution3', solution3(n[i]));
}
결과
반응형
'JavaScript > 메소드' 카테고리의 다른 글
[javascript] splice() 배열의 삭제,교체,추가 (0) | 2023.08.02 |
---|---|
[javascript] 오름차순 정렬 sort() (0) | 2023.07.29 |
[javascript] 정규식 ^ , + ,[], match() (0) | 2023.07.11 |
[javascript] 스트링으로 된 수식 계산 eval, new Function (0) | 2023.07.02 |
[javascirpt] trim, split, 정규식 + 공백 여러개 구분 (0) | 2023.07.02 |
댓글