Number difference after interchanging their first digits in JavaScript

We are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits.

For instance, for the array [105, 413]:

  • Original numbers: 105 and 413
  • After interchanging first digits: 405 and 113
  • The difference will be: |405 - 113| = 292

Algorithm

The approach involves converting numbers to strings, extracting first digits, creating new numbers with swapped first digits, and calculating the absolute difference.

Example

const arr = [105, 413];

const interchangedDigitDiff = (arr = []) => {
    // Convert numbers to strings for digit manipulation
    arr = arr.map(String);
    const [first, second] = arr;
    
    // Extract first digits
    const fChar = first[0];
    const sChar = second[0];
    
    // Create new numbers with interchanged first digits
    const newFirst = sChar + first.substring(1, first.length);
    const newSecond = fChar + second.substring(1, second.length);
    
    // Convert back to numbers and calculate absolute difference
    const newArr = [+newFirst, +newSecond];
    const diff = Math.abs(newArr[0] - newArr[1]);
    
    return diff;
};

console.log(interchangedDigitDiff(arr));
292

Step-by-Step Breakdown

Let's trace through the execution with [105, 413]:

const arr = [105, 413];

// Step 1: Convert to strings
const stringArr = arr.map(String);
console.log("String array:", stringArr);

// Step 2: Extract first digits
const firstDigit1 = stringArr[0][0];
const firstDigit2 = stringArr[1][0];
console.log("First digits:", firstDigit1, firstDigit2);

// Step 3: Create new numbers
const newNum1 = firstDigit2 + stringArr[0].substring(1);
const newNum2 = firstDigit1 + stringArr[1].substring(1);
console.log("New numbers:", newNum1, newNum2);

// Step 4: Calculate difference
const difference = Math.abs(+newNum1 - +newNum2);
console.log("Difference:", difference);
String array: [ '105', '413' ]
First digits: 1 4
New numbers: 405 113
Difference: 292

Alternative Implementation

Here's a more concise version using array destructuring:

const interchangedDifference = ([num1, num2]) => {
    const [str1, str2] = [String(num1), String(num2)];
    const newNum1 = +str2[0] + str1.slice(1);
    const newNum2 = +str1[0] + str2.slice(1);
    return Math.abs(newNum1 - newNum2);
};

// Test with different examples
console.log(interchangedDifference([105, 413])); // 292
console.log(interchangedDifference([123, 456])); // 333
console.log(interchangedDifference([789, 234])); // 555
292
333
555

Key Points

  • String() converts numbers to strings for digit extraction
  • substring() or slice() extracts remaining digits after the first
  • Unary plus operator + converts strings back to numbers
  • Math.abs() ensures positive difference regardless of order

Conclusion

This solution efficiently swaps first digits by converting numbers to strings, manipulating characters, and calculating the absolute difference. The approach works for any two positive integers.

Updated on: 2026-03-15T23:19:00+05:30

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements