
- 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
Finding all possible combined (plus and minus) sums of n arguments JavaScript
We are required to write a JavaScript function that in any number of arguments (all of Number type).
The function should calculate all possible sums of addition and subtraction.
For example − If the arguments are 1, 2, 3
Then all possible combinations are −
1 + 2 + 3 1 - 2 - 3 1 + 2 - 3 1 - 2 + 3
Finally, the function should the sum that is closest to 0. In this case, that answer would just be 0.
Example
const findSmallestPositive = (...arr) => { let set = new Set([Math.abs(arr[0])]); for (let i = 1; i < arr.length; i++){ const secondSet = new Set; for (let d of Array.from(set)){ secondSet.add(Math.abs(d + arr[i])) secondSet.add(Math.abs(d - arr[i])) }; set = secondSet; }; return Math.min(...Array.from(set)) }; console.log(findSmallestPositive(5,3)) console.log(findSmallestPositive(1,2,3)) console.log(findSmallestPositive(1,2,3,5))
Output
This will produce the following output −
2 0 1
- Related Articles
- Print all possible sums of consecutive numbers with sum N in C++
- Finding all possible subsets of an array in JavaScript
- Finding all possible ways of integer partitioning in JavaScript
- Finding all possible combinations from an array in JavaScript
- Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript
- Finding all possible prime pairs that sum upto input number using JavaScript
- All combinations of sums for array in JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Execute operations (plus, minus, multiply, divide) while updating a MySQL table?
- All possible permutations of N lists in Python
- Possible two sets from first N natural numbers difference of sums as D in C++
- Finding sum of all unique elements in JavaScript
- All possible numbers of N digits and base B without leading zeros?
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Generating all possible permutations of array in JavaScript

Advertisements