javascript
Parameters: num (integer)
A single positive or negative integer
Returns: boolean indicating whether the parameter is even
The checkEven function in Javascript is used to determine if a number is even or not. It takes an integer as input and returns true if the number is even and false if it's not.
Hello fellow programmer, hope you're having a great day! This post aims to walk you through the steps involved in crafting a simple JavaScript function called checkEven. The function we are going to create will be checking if a number is even or not. Coding can be tedious but equally rewarding once you get the hang of it. Buckle up and let's take a journey through the world of programming!
Step 1: Define the Function
The first step is to define the function. In JavaScript, you can define a function using the function
keyword followed by the function's name. In this case, let's name the function checkEven
. This function will take one parameter which is the number that we need to check whether it is even or not. In this step, we will just declare the function, we will not write its full logic yet.
function checkEven(num) {
// code comes here
}
Step 2: Implement the If Statement
Inside this function, we will use an if
statement to check if the number is even. In JavaScript, a number is even if it is divisible by 2 with no remainder. We can use the modulus %
operator for this purpose. If a number is even, the modulus of that number and 2 equals 0 (num % 2 === 0
).
function checkEven(num) {
if (num % 2 === 0) {
// code comes here
}
}
Step 3: Implement the Return Statement for Even Number
When the number is even, we need to return the true
value from the function to indicate that the input number is even. We can do this using the return
statement in JavaScript.
function checkEven(num) {
if (num % 2 === 0) {
return true;
}
}
Step 4: Implement the Return Statement for Odd Number
For the case when the number is not even (i.e., the number is odd), we need to return false
. We can simply add a return false;
statement after the if
statement from step 2. When the if
condition fails (which means the number is not even), JavaScript will execute this return false;
line.
function checkEven(num) {
if (num % 2 === 0) {
return true;
}
return false;
}
Step 5: Testing the checkEven function
It is always a good idea to test your functions to ensure that they work as expected. Let's test our checkEven
function with some sample numbers.
console.log(checkEven(2)); // should print true
console.log(checkEven(7)); // should print false
Conclusion:
Here is the full code of our checkEven
function. This function takes one number as input and returns true
if the number is even and false
if the number is odd.
function checkEven(num) {
if (num % 2 === 0) {
return true;
}
return false;
}
console.log(checkEven(2)); // true
console.log(checkEven(7)); // false
This completes the implementation of our checkEven
function. Using this function, you can now easily check if any number is even or odd in JavaScript.
This function uses the mathematical principle of division. According to this principle, a number is even if it can be divided by 2 with no remainder. To check this in our function, we use the modulo operator(%), which returns the remainder of a division: `if(number % 2 === 0)`.
Learn more