Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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');
}
like image 706
Evan Howington Avatar asked Dec 20 '18 03:12

Evan Howington


People also ask

How do you get a negative value in JavaScript?

To convert a positive number to a negative number, multiply the number by -1 , e.g. 5 * -1 . By multiplying the number by -1 , we flip the number's sign and get back a negative number. Copied!

How do you write negative 1 in JavaScript?

sign() The Math. sign() function returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument.

How do you make a number negative?

You cannot multiply a number by itself to get a negative number. To get a negative number, you need one negative and one positive number. The rule works the same way when you have more than two numbers to multiply or divide. An even number of negative numbers will give a positive answer.


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');
like image 151
Alexander O'Mara Avatar answered Sep 21 '22 12:09

Alexander O'Mara


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.

like image 35
Feathercrown Avatar answered Sep 22 '22 12:09

Feathercrown


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();
like image 34
Abana Clara Avatar answered Sep 19 '22 12:09

Abana Clara