How to useMath.floor() Method in Javascript

This method involves a combination of floor division and multiplication to isolate the last digit of each number. The steps are as follows:

  • Divide each number by 10 and take the integer part (floor division) to remove the last digit.
  • Multiply the result of the division by 10 to get a number ending in zero.
  • Subtract this number from the original number to isolate the last digit.
  • Compare the isolated last digits using the strict equality operator (===).
  • Return true if the last digits are the same, otherwise return false.

Example:

JavaScript
// Define function to check if two numbers have the same last digit
function haveSameLastDigit(num1, num2) {
    // Isolate the last digit using floor division and multiplication
    const lastDigit1 = num1 - Math.floor(num1 / 10) * 10;
    const lastDigit2 = num2 - Math.floor(num2 / 10) * 10;

    // Return if last digits are same or not
    return lastDigit1 === lastDigit2;
}

// Define two numbers
const number1 = 123;
const number2 = 453;

// Calling function with arguments
const result = haveSameLastDigit(number1, number2);

// Output the result
console.log(result);

Output
true


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.

Similar Reads

Methods to Check Both Numbers have the Same Last Digit

Table of Content Approach 1: Using the Modulus Operator (%)Approach 2: Converting Numbers to StringsApproach 3: Using the Array of DigitsApproach 4: Using the Bitwise AND Operator...

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....

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....

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....

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....

Approach 5: Using Math.floor() Method

This method involves a combination of floor division and multiplication to isolate the last digit of each number. The steps are as follows:...