Is there a way to force JavaScript to return a negative value in an alert box?

Viewed 648

JavaScript is returning X - Y , where X and Y are Real numbers and their sum is negative, instead of just the negative sum.

I've tried an if else statement using

if (Math.sign(function)<0)
else

where the if statement just had a "-" in front of the value to concatenate the string "minus" character in front of the number and the else statement was just a regular print out

function velocity_final(initial_velocity, acceleration, time)
{
    var initial_velocity = prompt('Please enter the Initial Velocity in Meters per Second');
    var acceleration = prompt('Please enter the acceleration in Meters per Second Squared');
    var time = prompt('Please enter the time in seconds');
    var final_velocity = initial_velocity + acceleration * time;
    alert('The Final Velocity is '+ final_velocity  + ' Meters Per Second');
}
3 Answers

prompt always returns a string, not a number. Even if the person enters a number, it will be a string that represents that number, not a number itself.

You will need to cast the results of prompt to a number before you can preform addition on it. When used with string, + is the concatenation operator, rather then the addition operator.

Somewhat confusingly, you can actually use an unary + for this purpose.

var initial_velocity = +prompt('Please enter the Initial Velocity in Meters per Second');
var acceleration = +prompt('Please enter the acceleration in Meters per Second Squared');
var time = +prompt('Please enter the time in seconds');
var final_velocity = initial_velocity + acceleration * time;
alert('The Final Velocity is '+ final_velocity  + ' Meters Per Second');

The + operator can be both addition and string concatenation. When the prompt box returns, it gives you back a string. String + number = string, so it concatenates (joins) the two values together instead of adding them. To fix this, you can convert the string to a number using a single + operator (and some parentheses if you want) to convert the string to a number, like so:

function velocity_final()
{
    var initial_velocity = prompt('Please enter the Initial Velocity in Meters per Second');
    var acceleration = prompt('Please enter the acceleration in Meters per Second Squared');
    var time = prompt('Please enter the time in seconds');
    var final_velocity = (+initial_velocity) + (+acceleration) * (+time);
    alert('The Final Velocity is '+ final_velocity  + ' Meters Per Second');
}

console.log(velocity_final());

You could also convert the values as soon as the prompt returns a value, if you wanted.

PS: I removed the function parameters, as you set them manually anyways rather than passing anything in. If you do end up passing in values later rather than asking the user for them, you'll need to add those back into the function statement for them to be properly passed in.

Prompt returns a string. Such that doing "1" +"-1" will result to "1-1" due to concatenation. Why "1" + "1" becomes 2 in the printed output is because of how Javascript automatically attempts to parse strings into numbers, if the evaluated strings contains a character, it gets concatenated instead. You need to explicitly cast the numbers.

You can use Number(), you can multiply each string to 1 to automatically convert them, you can use parseInt(), or you can use + preceding the returned values as shown by the other answers here. I would use the first one I mentioned in the example below.

function velocity_final()
{
    var initial_velocity = prompt('Please enter the Initial Velocity in Meters per Second');
    var acceleration = prompt('Please enter the acceleration in Meters per Second Squared');
    var time = prompt('Please enter the time in seconds');
    var final_velocity = Number(initial_velocity) + Number(acceleration) * Number(time);
    alert('The Final Velocity is '+ final_velocity  + ' Meters Per Second');
}

velocity_final();

Related