JavaScript Program to Check if the Numbers Have Same Last Digit

Updated on September 30, 2024
Check if the Numbers Have Same Last Digit header image

Introduction

Comparing the last digits of numbers can be a frequent requirement in various programming tasks, such as numerical validations or matching specific criteria in a data set. In JavaScript, this kind of operation is straightforward thanks to the language's flexibility in handling numbers and strings.

In this article, you will learn how to write a JavaScript function to check if two or more numbers have the same last digit. Explore practical examples that demonstrate how to implement and utilize this function effectively in different scenarios.

Writing the Basic Function

Define the Function

  1. Start by creating a function named haveSameLastDigit. This function will accept an arbitrary number of arguments.

  2. Inside the function, use the Array.prototype.every method to ensure every number meets a specific condition.

  3. Convert each number to a string and compare the last character of these strings.

    javascript
    function haveSameLastDigit(...numbers) {
        return numbers.every((num, _, arr) => num.toString().slice(-1) === arr[0].toString().slice(-1));
    }
    

    In this code, ...numbers uses the rest parameter syntax to gather any number of arguments into an array. The every method checks whether all numbers have the same last digit as the first number in the input array.

Example Usage of the Basic Function

  1. Test the function with numbers that have the same last digit.

  2. Test the function with numbers with different last digits.

    javascript
    console.log(haveSameLastDigit(27, 537, 17)); // true
    console.log(haveSameLastDigit(234, 78, 19)); // false
    

    These examples show how the function is used to check the last digit of provided numbers and returns true if they all match, and false otherwise.

Extending the Functionality

Modify Function to Handle Edge Cases

  1. Consider adding input validation to ensure the function handles different data types and edge cases gracefully.

  2. Return false if input arguments are not numbers or the array is empty.

    javascript
    function haveSameLastDigit(...numbers) {
        if (numbers.length === 0 || numbers.some(num => typeof num !== 'number')) {
            return false;
        }
        return numbers.every((num, _, arr) => num.toString().slice(-1) === arr[0].toString().slice(-1));
    }
    

    The updated function now checks for the correct data type and ensures that it does not proceed with comparisons if there's invalid input or no numbers at all.

Handle Numeral Systems Beyond Decimal

  1. Adapt the function to work with numbers in different bases, such as hexadecimal or binary.

  2. Allow an additional parameter specifying the base, and adjust the string conversion accordingly.

    javascript
    function haveSameLastDigit(base = 10, ...numbers) {
        if (numbers.length === 0 || numbers.some(num => typeof num !== 'number')) {
            return false;
        }
        return numbers.every((num, _, arr) => num.toString(base).slice(-1) === arr[0].toString(base).slice(-1));
    }
    

    Here, an optional base parameter has been added. The function now converts numbers into strings according to the specified base before comparing their last digits.

Conclusion

The ability to check if numbers share the same last digit in JavaScript is a useful skill, especially in data validation or when processing different numeral systems. The examples provided illustrate a flexible approach to achieving this, allowing for a better understanding of string manipulation and condition checking in JavaScript. By customizing this function, you ensure that it can adapt to various requirements and input types, making your JavaScript code more robust and adaptable to changing needs.