Start learning free

LESSON 55 OF 410 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in JavaScript

Practice Read the full explanation

Given a non-negative integer n with at least two digits, compute and return the sum of the last two digits

Example:

Input: n = 2379

Output: 16

Explanation:
Last two digits of n are 7 and 9, so our answer is 7 + 9 = 16
Hints:

You can make use of modulo operator

You can get the last digit of a number using modulo 10:
2379 % 10 = 9

You can get a number consisting of last 2 digits using modulo 100:
2379 % 100 = 79

If you have a 2 digit number, you can get the first digit by using integer division

Divide a 2 digit number to 10 to get the first digit:
79 / 10 = 7 (integer division)
STEP 1 OF 4
    Ctrl + Enter to check

    JAVASCRIPT · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to extract the last two digits of a given number and compute their sum. This problem is significant in various applications such as checksum calculations, digital root computations, and more. A common pitfall is to overcomplicate the extraction of the last two digits, but with the right approach, it can be simplified.

    Approach

    To solve this problem, we can use the modulo operator to extract the last two digits of the number. Here's a step-by-step approach:

    1. Use the modulo operator to get the last two digits of the number.
    2. Extract the last digit using modulo 10.
    3. Extract the second last digit by dividing the last two digits by 10.
    4. Sum the two digits and return the result.

    Naive Solution

    A naive solution might involve converting the number to a string, extracting the last two characters, converting them back to integers, and summing them. However, this approach is not optimal due to the overhead of string operations.

    Optimized Solution

    The optimized solution leverages the modulo operator to directly extract the last two digits and perform arithmetic operations. This approach is efficient and has a constant time complexity of O(1).

    Algorithm

    1. Compute the last two digits using n % 100.
    2. Extract the last digit using lastTwoDigits % 10.
    3. Extract the second last digit using Math.floor(lastTwoDigits / 10).
    4. Sum the two digits and return the result.

    Code Implementation

    /**
     * Function to compute the sum of the last two digits of a given number
     * @param {number} n - A non-negative integer with at least two digits
     * @returns {number} - Sum of the last two digits
     */
    function sumLastTwoDigits(n) {
      // Get the last two digits
      const lastTwoDigits = n % 100;
      
      // Extract the last digit
      const lastDigit = lastTwoDigits % 10;
      
      // Extract the second last digit
      const secondLastDigit = Math.floor(lastTwoDigits / 10);
      
      // Return the sum of the last two digits
      return lastDigit + secondLastDigit;
    }
    
    // Example usage
    const n = 2379;
    console.log(sumLastTwoDigits(n)); // Output: 16
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the operations performed (modulo and division) are constant time operations. The space complexity is also O(1) as we are using a fixed amount of extra space.

    Edge Cases

    Potential edge cases include:

    • Minimum two-digit number (e.g., 10)
    • Numbers with trailing zeros (e.g., 100, 2300)

    Each of these cases is handled effectively by the algorithm.

    Testing

    To test the solution comprehensively, consider the following test cases:

    console.log(sumLastTwoDigits(2379)); // Output: 16
    console.log(sumLastTwoDigits(10));   // Output: 1
    console.log(sumLastTwoDigits(100));  // Output: 0
    console.log(sumLastTwoDigits(2300)); // Output: 0
    console.log(sumLastTwoDigits(99));   // Output: 18
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller, manageable parts.
    • Leverage mathematical operations to simplify the solution.
    • Think about edge cases and how to handle them.
    • Practice similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to compute the sum of the last two digits of a given number using an efficient approach. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free