Number Is Odd

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore the concept of determining whether a given number is odd or even in JavaScript. We will use the modulo operator to check the remainder when the number is divided by 2. This lab will help you understand the basics of arithmetic operators in JavaScript and how to use them to write simple programs.


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-28433{{"`Number Is Odd`"}} javascript/data_types -.-> lab-28433{{"`Number Is Odd`"}} javascript/arith_ops -.-> lab-28433{{"`Number Is Odd`"}} javascript/comp_ops -.-> lab-28433{{"`Number Is Odd`"}} end

Check if a Number is Odd

To practice coding, open the Terminal/SSH and type node. Use the modulo (%) operator to check if a number is odd or even. If the result is 1, the number is odd, and if the result is 0, the number is even. Return true if the number is odd and false if the number is even.

const isOdd = (num) => num % 2 === 1;

Example usage:

isOdd(3); // true

Summary

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

Other JavaScript Tutorials you may like