
- 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
Sum a negative number (negative and positive digits) - JavaScript
We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits
For example −
-234 --> -2 + 3 + 4 = 5 -54 --> -5 + 4 = -1
Let’s write the code for this function −
Example
Following is the code −
const num = -4345; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { if(ind === 0){ return acc; } if(ind === 1){ acc -= +val; return acc; }; acc += +val; return acc; }, 0); }; console.log(sumNum(num));
Output
Following is the output in the console −
8
- Related Articles
- Negative number digit sum in JavaScript
- Reversing negative and positive numbers in JavaScript
- Write a positive and negative integer whose sum is $-$7.
- Java Program to convert positive int to negative and negative to positive
- Write a positive and a negative integer whose sum is $-7$.
- How to convert a negative number to a positive one in JavaScript?
- Why negative multiplied by negative is positive?
- Distinguish positive and negative numbers.
- Schizophrenia Symptoms: Positive and Negative?
- Python - Sum negative and positive values using GroupBy in Pandas
- Counting number of positive and negative votes in MySQL?
- Why is there no zero positive and negative number?
- Implement Bubble sort with negative and positive numbers – JavaScript?
- (a) Write a negative integer and a positive integer whose sum is $-5$.(b) Write a negative integer and a positive integer whose difference is $-3$.
- Positive and Negative Effects of Daydreaming

Advertisements