JavaScript Confusion


  • 0
    C

    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_1497083790344_62bae070-850d-4879-a4b8-6be6054ad0b7-image.png


Log in to reply
 

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