Saturday, July 13, 2024
HomeLanguagesJavascriptJavaScript Program to Check if Two Numbers have Same Last Digit

JavaScript Program to Check if Two Numbers have Same Last Digit

In this article, we will discuss how to check if two numbers have the same last digit in JavaScript. Checking the last digit of a number is a common requirement in programming tasks, and it can be useful in various scenarios. We will explore an approach using JavaScript to accomplish this task.

Methods to Check Both Numbers have the Same Last Digit

  • Using the Modulus Operator (%)
  • Converting Numbers to Strings using toString() Method
  • Using the Array of Digits using the Spread Operator
  • Using the Bitwise AND Operator

We will explore every approach for checking if the Numbers Have the Same Last Digit, along with understanding their basic implementations.

Approach 1: Using the Modulus Operator (%)

  • Take the modulus (%) of both numbers with 10 to extract their last digits.
  • Compare the last digits using the ===.
  • Return true if the last digits are the same, otherwise return false.

Example:

Javascript




// Define function to check last digit of number
function haveSameLastDigit(num1, num2) {
  
    // Getting last digit using % operator
    const lastDigit1 = num1 % 10;
    const lastDigit2 = num2 % 10;
  
    // Return if last digits are same or not
    return lastDigit1 === lastDigit2;
}
  
const number1 = 123;
const number2 = 456;
  
// Calling function with arguements
const result = haveSameLastDigit(number1, number2);
  
// Giving output
console.log(result);


Output

false

Approach 2: Converting Numbers to Strings

  • Convert both numbers to strings using the toString() method.
  • Get the last character (digit) of each string using the slice() method.
  • Compare the last digits using the ===.
  • Return true if the last digits are the same, otherwise return false.

Syntax:

string.slice(startIndex, endIndex);

Example:

Javascript




// Define function to check last digit of number
function haveSameLastDigit(num1, num2) {
  
    // Getting last digit using % operator
    const lastDigit1 = num1.toString().slice(-1);
    const lastDigit2 = num2.toString().slice(-1);
  
    // Return if last digits are same or not
    return lastDigit1 === lastDigit2;
}
  
const number1 = 123;
const number2 = 453;
  
// Calling function with arguements
const result = haveSameLastDigit(number1, number2);
  
// Giving output
console.log(result);


Output

true

Approach 3: Using the Array of Digits

  • Create an array of digits for each number using the spread operator.
  • Get the last digit of each array using the array index.
  • Compare the last digits using the ===.
  • Return true if the last digits are the same, otherwise return false.

Syntax:

// Spread operator 
[...iterable]

Example:

Javascript




// Define function to check last digit of number
function haveSameLastDigit(num1, num2) {
  
    // Converting number to string
    const digits1 = [...num1.toString()];
    const digits2 = [...num2.toString()];
      
    // Getting last character of string
    const lastDigit1 = digits1[digits1.length - 1];
    const lastDigit2 = digits2[digits2.length - 1];
  
    // Return if last digits are same or not
    return lastDigit1 === lastDigit2;
}
  
const number1 = 123;
const number2 = 453;
  
// Calling function with arguements
const result = haveSameLastDigit(number1, number2);
  
// Giving output
console.log(result);


Output

true

Approach 4: Using the Bitwise AND Operator

  • The Bitwise AND operator & to perform a bitwise AND operation between the numbers and 1 (num1 & 1 and num2 & 1).
  • The result will be 1 if the last bit (last digit) of both numbers is the same, and 0 otherwise.
  • Then, we compare the results using the strict equality operator === to check if they are equal.
  • If they are equal, it means the numbers have the same last digit.

Syntax:

firstNum=num&1

Javascript




function hasSameLastDigit(num1, num2) {
  
    // Get the last bit of num1
    const lastDigit1 = num1 & 1;
  
    // Get the last bit of num2
    const lastDigit2 = num2 & 1;
    return lastDigit1 === lastDigit2;
}
  
// Example usage:
const number1 = 123;
const number2 = 453;
const result = hasSameLastDigit(number1, number2);
console.log(result); // Output: false


Output

true

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments