Number Is Power of Ten

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to determine whether a given number is a power of 10 or not using JavaScript. We will use the Math.log10() function and the modulo operator to check if the number is a power of 10. By the end of this lab, you will have a better understanding of how to work with logarithmic functions and the modulo operator in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("JavaScript")) -.-> javascript/BasicConceptsGroup(["Basic Concepts"]) javascript/BasicConceptsGroup -.-> javascript/variables("Variables") javascript/BasicConceptsGroup -.-> javascript/data_types("Data Types") javascript/BasicConceptsGroup -.-> javascript/arith_ops("Arithmetic Operators") javascript/BasicConceptsGroup -.-> javascript/comp_ops("Comparison Operators") subgraph Lab Skills javascript/variables -.-> lab-28435{{"Number Is Power of Ten"}} javascript/data_types -.-> lab-28435{{"Number Is Power of Ten"}} javascript/arith_ops -.-> lab-28435{{"Number Is Power of Ten"}} javascript/comp_ops -.-> lab-28435{{"Number Is Power of Ten"}} end

Check If a Number Is a Power of Ten

To check if a number is a power of ten, open the Terminal/SSH and type node.

Here's the code you can use to determine if n is a power of 10:

const isPowerOfTen = (n) => Math.log10(n) % 1 === 0;

Use isPowerOfTen() function to determine whether a given number is a power of ten.

isPowerOfTen(1); // true
isPowerOfTen(10); // true
isPowerOfTen(20); // false

Summary

Congratulations! You have completed the Number Is Power of Ten lab. You can practice more labs in LabEx to improve your skills.