Open In App

JavaScript Program to Add n Binary Strings

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about Adding n binary strings by using JavaScript. Adding n binary strings in JavaScript refers to the process of performing binary addition on a collection of n binary strings, treating them as binary numbers, and producing the sum in binary representation as the result.

There are several methods that can be used to Add n binary strings by using javascript, which is listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using for…in loop

In this approach, The custom function sums binary strings in inputStr using a for…in loop, converting them to decimal, and returning the result as a binary string

Syntax:

for (let i in obj1) {
// Prints all the keys in
// obj1 on the console
console.log(i);
};

Example: In this example,The addBinaryStrings function takes an array of binary strings, converts them to decimal, adds them together, and returns the sum as a binary string.

Javascript




function addBinaryStrings(str1) {
    let result = 0;
  
    for (let i in str1) {
        result += parseInt(str1[i], 2);
    }
  
    return result.toString(2);
}
  
let inputStr = ['0111', '1001'];
let sum = addBinaryStrings(inputStr);
console.log(sum);


Output

10000

Approach 2: Using reduce() method

In this approach, using Array.reduce(), define a function that takes an array of binary strings, converts them to decimal, accumulates their sum, and returns the result as a binary string. The accumulator starts at “0”.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), 
initialValue );

Example: In this example,the addingBinaryStr function takes an array of binary strings, converts them to decimal, adds them with reduce, and returns the sum as a binary string.

Javascript




function addingBinaryStr(str1) {
    return str1.reduce((val1, val2) => {
        let binary1 = parseInt(val1, 2);
        let binary2 = parseInt(val2, 2);
        let sum = binary1 + binary2;
        return sum.toString(2);
    }, "0");
}
  
let inputStr = ['0111', '1001'];
let result = addingBinaryStr(inputStr);
console.log(result);


Output

10000

Approach 3 : Using parseInt() and toString() method

In this approach,we Add binary strings by converting them to integers using parseInt with base 2, then summing them, and finally converting the result back to binary using toString(2).

Syntax:

parseInt(Value, radix)   //parseInt()
num.toString(base) //toString()

Example: In this example, we are using above-explained approach.

Javascript




let binary1 = "0111";
let binary2 = "1001";
  
// Parse binary string 'binary2' to an integer
let num1 = parseInt(binary1, 2);
// Parse binary string 'binary2' to an integer
let num2 = parseInt(binary2, 2);
// Add the two integers
let sum = num1 + num2;
// Convert the sum back to a binary string
let result = sum.toString(2);
  
console.log(result);


Output

10000


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads