From 1901818572eaafbd98a8b7e7cd676b6d374ae9e7 Mon Sep 17 00:00:00 2001 From: hgarg1 Date: Fri, 31 May 2019 11:16:41 +0530 Subject: [PATCH] added binary sum to int --- src/binarySum.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/binarySum.js diff --git a/src/binarySum.js b/src/binarySum.js new file mode 100644 index 0000000..2357c73 --- /dev/null +++ b/src/binarySum.js @@ -0,0 +1,22 @@ +// Sum two integers (Bin1 + Bin2) represented as binary numbers and return decimal value + +var ConvertBase = function (num) { + return { + from : function (baseFrom) { + return { + to : function (baseTo) { + return parseInt(num, baseFrom).toString(baseTo); + } + }; + } + }; +}; + +// binary to decimal +ConvertBase.bin2dec = function (num) { + return ConvertBase(num).from(2).to(10); +}; + +export const binaryDecimalSum = (Bin1, Bin2) => { + return (ConvertBase.bin2dec(Bin2) + ConvertBase.bin2dec(Bin2)); +} \ No newline at end of file