- 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

We are required to write a JavaScript function that takes in two numbers.

Our function should count the number of carries we are required to take while adding those numbers as if we were adding them on paper.

Like in the following image while adding 179 and 284, we had used carries two times, so for these two numbers, our function should return 2.

Following is the code −

const num1 = 179; const num2 = 284; const countCarries = (num1 = 1, num2 = 1) => { let res = 0; let carry = 0; while(num1 + num2){ carry = +(num1 % 10 + num2 % 10 + carry > 9); res += carry; num1 = num1 / 10 | 0; num2 = num2 / 10 | 0; }; return res; }; console.log(countCarries(num1, num2));

Following is the console output −

2

- Related Questions & Answers
- Count the number of carry operations required to add two numbers in C++
- Adding two Sets in Javascript
- Recursively adding digits of a number in JavaScript
- Adding only odd or even numbers JavaScript
- Digit distance of two numbers - JavaScript
- While adding the numbers contained in quotes, how MySQL evaluates if we write non-numeric text after a number?
- While adding the numbers contained in quotes, how MySQL evaluates if we write non-numeric text before a number?
- Maximum sum by adding numbers with same number of set bits in C++
- Adding base n numbers
- Find out the GCD of two numbers using while loop in C language
- While adding the numbers contained in quotes, how MySQL evaluates if we write non-numeric text between numbers of a string?
- Finding two prime numbers with a specific number gap in JavaScript
- Adding numbers represented by string without complete conversion in JavaScript
- Set conditions while adding column values in MySQL?
- Finding two golden numbers in JavaScript

Advertisements