Javascript Everywhere

Help for javascript developer

Javascript Everywhere in the world

Sunday, May 03, 2020

Javascript Challenges: Sum of two value without using +(plus) sign

sum of two value without using + sign
Write a program for the above logic

Introduction:
Developer requests us to find a solution for this challenge. Challenge is the sum of two values without using plus sign using javascript. Developer are really in to solve challenges and give multiple solutions.

Challenge:

   Write a logic for below input:
Input Output
getSumOfVal(10, 20) 30
getSumOfVal(10, -10) 0
getSumOfVal(5, 8) 13
getSumOfVal(0, 0) 0

Program Description: Write a program sum of two value without a plus sign.

The program output is: 1)

function getSumOfVal(a, b) {
    if (b == 0) {
        return a;
    } else {
        return getSumOfVal(a ^ b, (a & b) << 1)
    }
};
Explanation:
-> This is a program performs using a bitwise operator.
->

function getSumOfVal(a, b) {
    if (b == 0) {
        return a;
    } else {
        return getSumOfVal(10 ^ -10, (10 & -10) << 1)
        // 10 ^ -10 = -4 
        // (10 & -10) << 1 = 4
        // -4 + 4 = 0
    }
};
-> It's simple if you want to understand more logic then contact me I will explain in details.

2)
const getSumOfVal = (a,b) => b ? getSumOfVal(a ^ b, (a & b) << 1) : a;
Explanation:
-> Same as above just use ES6 syntax here.
-> You can write a whole program in a single line as well.

3)
const getSumOfVal = (a, b) => eval(''.concat(a).concat(String.fromCharCode(0x2B)).concat(b));
-> Here use plus sign in character code. so don't be confuse about string and fromCharCode

4)
function getSumOfVal(x, y) {
    return Math.log2(2**x * 2**y);
}
Explanation:
-> Here we can use the log2 method for sum. Math is a default javascript library.

5)
function getSumOfVal(a, b) {
    return a - - b;
}
Explanation:
-> Very basic and simple method for sum.
-> Many developers give solutions using this method and we congratulate those use this method.
-> This method is very easy to use.
-> Rule is  ++ = +, -+ = -, --=+, +-=-
-> In-word: plus, plus = plus, minus, plus = minus, plus, minus = minus, minus, minus = plus
6) Example: 10 - - 10 = 20, 10 - 10 = 0

For this blog, my purpose is just clear developer logic and improve your skill and fundamentals of operator provided by javascript and another language. In my Fb(javascript everywhere) page many developers failed to solve this because he only thinks complex.

If you like to give challenges to the developer then contact me I will create a blog and post with your name and share it.

Other: Math series solve using javascript.
Maths series solve using javascript