JavaScript Confusion

  • 0

    Here is my code:

    var complexNumberMultiply = function(a, b) {
        var a1 = parseInt(a),
        	b1 = parseInt(b),
        	c = a1*a1-b1*b1,
        	d = a1*b1*2;
        	if(c<0){str = '-'+Math.abs(c)+'+'+d+'i';}
    	else if(d<0){str = c+'+'+'-'+Math.abs(d)+'i';}
    	else{str = c+'+'+d+'i';}
        return str;

    When the input is "1+-1i", I console.log() the output in Chrome. It is "0+-2i", which is the expected result.
    But when I submit, it shows the output is "0+2i"!
    I've tried several methods but still don't know where I am wrong.Can anyone tell me why? Thanks a lot!

  • 0

    parseInt('1+-1i') === 1;
    The example is just a coincident.
    You need get 'a' and 'b' from 'a+bi' and use them for later calculation.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.