0

Advance thanks, Question looks like simple but i could not able to find the solution.

function sumStrings(a, b)
{
return  +a + +b;
}
sumStrings('1','2') //=>3

Expected output

sumStrings('1','2') // => '3'
4
  • 1
    Try this console.log("x='5'"); Commented Jul 6, 2017 at 7:02
  • You could try to convert it to string: var n = x.toString(); Commented Jul 6, 2017 at 7:05
  • 1
    I'm not sure what you are trying to achieve... x='5' or x="5" in JavaScript have the same result... You are talking about a form? I don't see any form... Commented Jul 6, 2017 at 7:06
  • Thanks Hassan,I have modified the question Commented Jul 6, 2017 at 7:48

4 Answers 4

2

After addition, add ' to beginning and end.

function sumStrings(a, b){
  
  return  "'" + (Number(a) + Number(b)) + "'";
}
console.log(sumStrings('1','2'));

Sign up to request clarification or add additional context in comments.

6 Comments

Looks good the expected result will shown in console,But if i call the functionn like sumStrings('1','2'); it returns "'3'" ,How can we make it as '3'
@murthynaikak Where are you storing the result?
I am not storing anywhere actually i was executing in console so it treats as a string,the same if i place in my js file it works as per my expectation. I have one more query is How can i achieve the result like this sumStrings('50095301248058391139327916261', '81055900096023504197206408605') // => '131151201344081895336534324866'
In console if you execute sumStrings('50095301248058391139327916261', '81055900096023504197206408605') then it would return "'1.3115120134408189e+29'". "" signifies the returned result is string. So, your output is correct, it's just browser way of telling a string.
You are correct sumStrings('50095301248058391139327916261', '81055900096023504197206408605') then it would return "'1.3115120134408189e+29'" ,I dont want exponential .i want the result should be '131151201344081895336534324866'
|
1
function sumStrings(a, b) {
    // Separate decimal part here
    var pointa = a.indexOf(".");
    var pointb = b.indexOf(".");
    var deca = pointa != -1 ? a.substring(pointa + 1) : "0";
    var decb = pointb != -1 ? b.substring(pointb + 1) : "0";

    if (deca.length < decb.length)
        deca += (Math.pow(10, decb.length - deca.length)).toString().substring(1);
    else
        decb += (Math.pow(10, deca.length - decb.length)).toString().substring(1);

    var inta = pointa != -1 ? a.substring(0, pointa) : a;
    var intb = pointb != -1 ? b.substring(0, pointb) : b;
    // console.log(deca + " " + decb);
    var decc = addBigInt(deca, decb);
    var intc = addBigInt(inta, intb);

    if (decc.length > deca.length) {
        intc = addBigInt(intc, "1");
        decc = decc.substring(1);
    }

    var lastZero = decc.length - 1;
    while (lastZero >= 0 && decc[lastZero] == "0") {
        lastZero--;
    }

    if (lastZero >= 0)
        return intc + "." + decc.substring(0, lastZero + 1);
    else
        return intc;
}

function addBigInt(a, b) {
    var inda = a.length - 1;
    var indb = b.length - 1;
    var c = [];
    const zero = "0".charCodeAt(0);
    var carry = 0;
    var sum = 0;

    while (inda >= 0 && indb >= 0) {
        var d1 = a.charCodeAt(inda--) - zero;
        var d2 = b.charCodeAt(indb--) - zero;

        sum = (d1 + d2 + carry);
        carry = Math.floor(sum / 10);
        sum %= 10;
        c.unshift(sum);
    }

    if (inda >= 0) {
        while (carry && inda >= 0) {
            sum = a.charCodeAt(inda--) - zero + carry;
            c.unshift(sum % 10);
            carry = Math.floor(sum / 10);
        }
        c.unshift(a.substring(0, inda + !carry));
    } else {
        while (carry && indb >= 0) {
            sum = b.charCodeAt(indb--) - zero + carry;
            c.unshift(sum % 10);
            carry = Math.floor(sum / 10);
        }
        c.unshift(b.substring(0, indb + !carry));
    }

    if (carry)
        c.unshift(carry);
    return c.join("");
}
  console.log(sumStrings("1","2"));
  console.log(sumStrings("800","9567"));
  console.log(sumStrings("99.1","1"));
  console.log(sumStrings("00103","08567"));
  console.log(sumStrings("50095301248058391139327916261.5","81055900096023504197206408605"));

Comments

0

If you want to escape the single quote, you can try to add a backslash before the single quote. i.e.

var x = '\'5\'';

1 Comment

Thanks for your ans ,But still var x = '\'5\''; x "'5'" ,i am expecting output as '5'
0

With the new template literal you can try this:

a=`'5'`;
console.log(a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.