
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Difference of digits at alternate indices in JavaScript
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits odd place.
Example
The code for this will be −
const num = 123456; const alternateDifference = (num, res = 0, ind = 0) => { if(num){ if(ind % 2 === 0){ res += num % 10; }else{ res -= num % 10; }; return alternateDifference(Math.floor(num / 10), res, ++ind); }; return Math.abs(res); }; console.log(alternateDifference(num));
Output
The output in the console −
3
- Related Articles
- Pair of similar elements at different indices in JavaScript
- Absolute difference of corresponding digits in JavaScript
- Product sum difference of digits of a number in JavaScript
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- Difference between product and sum of digits of a number in JavaScript
- Number difference after interchanging their first digits in JavaScript
- Alternate casing a string in JavaScript
- Alternate addition multiplication in an array - JavaScript
- Python program to remove elements at Indices in List
- Matching odd even indices with values in JavaScript
- Python – Find the sum of Length of Strings at given indices
- Finding product of Number digits in JavaScript
- Repeated sum of Number’s digits in JavaScript
- Separating digits of a number in JavaScript
- Returning a decimal that have 1s in its binary form only at indices specified by array in JavaScript

Advertisements