
- 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
Algorithm to add binary arrays in JavaScript
Basics of Binary Addition −
The four rules of binary addition are −
0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10
Keeping these points in mind, binary addition is very similar to the decimal addition (that follows the carry principle).
We are required to write a JavaScript function that takes in two arrays that contains only binary strings. (either '0' or '1').
The function should add the corresponding binary bits from the arrays and return a new array that contains the additional result of those arrays.
For example − If the input arrays are −
const arr1 = ['1', '0', '1']; const arr2 = ['1', '0', '1'];
Then the output should be −
const output = ['1', '0', '1', '0'];
Example
The code for this will be −
const arr1 = ['1', '0', '1']; const arr2 = ['1', '0', '1']; const addBinary = (arr1 = [], arr2 = []) => { const str1 = arr1.join(''); const str2 = arr2.join(''); let carry = 0, temp = 0, res = ''; for(let i = Math.max(str1.length, str2.length) − 1; i >= 0; i−−){ const el1 = +str1[i] || 0; const el2 = +str2[i] || 0; if(el1 + el2 + carry > 1){ temp = 0; carry = 1; }else{ temp = el1 + el2 + carry; carry = 0; }; res = temp + res; }; if(carry){ res = carry + res; }; return res.split(''); }; console.log(addBinary(arr1, arr2));
Output
And the output in the console will be −
[ '1', '0', '1', '0' ]
- Related Articles
- How to add two arrays into a new array in JavaScript?
- How Binary Classification Tree Algorithm Works
- Haskell program to add binary strings
- Program to add two binary strings in C++
- How to add Two Binary Strings in Golang?
- String to binary in JavaScript
- Fuzzy Search Algorithm in JavaScript
- How to create arrays in JavaScript?
- How to compare arrays in JavaScript?
- How to reduce arrays in JavaScript?
- Add n binary strings?
- Add n binary strings in C++?
- How to add binary numbers using Python?
- Java Program to Add Two Binary Strings
- Program to add two binary strings, and return also as binary string in C++

Advertisements