Divisibility Check in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will dive into the world of JavaScript programming and learn how to create a function that checks if a number is divisible by another number. Through this lab, you will gain a deeper understanding of the modulo operator and how it can be used to perform calculations in JavaScript. By the end of this lab, you will have a functional and efficient solution to determine if one number is divisible by another.


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-28417{{"`Divisibility Check in JavaScript`"}} javascript/data_types -.-> lab-28417{{"`Divisibility Check in JavaScript`"}} javascript/arith_ops -.-> lab-28417{{"`Divisibility Check in JavaScript`"}} javascript/comp_ops -.-> lab-28417{{"`Divisibility Check in JavaScript`"}} end

Check if a Number is Divisible

To check if a number is divisible by another number in JavaScript, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the modulo operator (%) to check if the remainder of the division is equal to 0. If it is, then the number is divisible.

Here's an example function that checks if the first numeric argument is divisible by the second one:

const isDivisible = (dividend, divisor) => dividend % divisor === 0;

You can test this function with isDivisible(6, 3), which should return true since 6 is divisible by 3.

Summary

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

Other JavaScript Tutorials you may like