Introduction
In this lab, we will delve into the world of JavaScript programming and learn how to write a function to determine whether a given number is even or odd. The lab will guide you through the steps of using the modulo operator to check whether a number is divisible by 2, and then returning true if it is even, or false if it is odd. By the end of this lab, you will have a better understanding of basic programming concepts in JavaScript and how to write functions for simple tasks.
Check if a Number is Even
To practice coding, open the Terminal/SSH and type node. Use the following code to check if a number is even or odd:
const isEven = (num) => num % 2 === 0;
The code above uses the modulo (%) operator to check whether a number is odd or even. If the number is even, the function returns true. If it's odd, the function returns false.
Here is an example of how to use the isEven function:
isEven(3); // returns false
Summary
Congratulations! You have completed the Number Is Even lab. You can practice more labs in LabEx to improve your skills.