

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Number difference after interchanging their first digits in JavaScript
Problem
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],
The difference will be: |405 - 113| = 292
Example
Following is the code −
const arr = [105, 413]; const interchangedDigitDiff = (arr = []) => { arr = arr.map(String); const [first, second] = arr; const fChar = first[0]; const sChar = second[0]; const newFirst = sChar + first.substring(1, first.length); const newSecond = fChar + second.substring(1, second.length); const newArr = [+newFirst, +newSecond]; const diff = Math.abs(newArr[0] - newArr[1]); return diff; }; console.log(interchangedDigitDiff(arr));
Output
Following is the console output −
292
- Related Questions & Answers
- Smallest number after removing n digits in JavaScript
- Interchanging first letters of words in a string in JavaScript
- Product sum difference of digits of a number in JavaScript
- Count number of digits after decimal on dividing a number in C++
- Difference between product and sum of digits of a number in JavaScript
- Interchanging a string to a binary string in JavaScript
- Absolute difference of corresponding digits in JavaScript
- Finding product of Number digits in JavaScript
- Separating digits of a number in JavaScript
- Number Split into individual digits in JavaScript
- Returning number with increasing digits. in JavaScript
- Difference of digits at alternate indices in JavaScript
- Number of digits that divide the complete number in JavaScript
- Recursively adding digits of a number in JavaScript
- Prime digits sum of a number in JavaScript
Advertisements