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











up vote
7
down vote

favorite
1












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');
}









share|improve this question









New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
    – Evan Howington
    1 hour ago








  • 1




    Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
    – Abana Clara
    1 hour ago















up vote
7
down vote

favorite
1












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');
}









share|improve this question









New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
    – Evan Howington
    1 hour ago








  • 1




    Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
    – Abana Clara
    1 hour ago













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





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');
}









share|improve this question









New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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');
}






javascript






share|improve this question









New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Alexander O'Mara

42.9k1395128




42.9k1395128






New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









Evan Howington

384




384




New contributor




Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Evan Howington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
    – Evan Howington
    1 hour ago








  • 1




    Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
    – Abana Clara
    1 hour ago


















  • for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
    – Evan Howington
    1 hour ago








  • 1




    Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
    – Abana Clara
    1 hour ago
















for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
– Evan Howington
1 hour ago






for the prompts I put in 32, -9.81, 10 and it returns 32-98.100000 instead of just a negative number
– Evan Howington
1 hour ago






1




1




Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
– Abana Clara
1 hour ago




Please do take the time to format your sample code and you do not have to follow up content on a comment, edit the post instead. You get unlimited edits on your post so feel free to use as much as you want.
– Abana Clara
1 hour ago












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










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();








share|improve this answer























  • All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
    – Evan Howington
    1 hour ago




















up vote
6
down vote













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');





share|improve this answer






























    up vote
    1
    down vote













    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.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Evan Howington is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53862185%2fis-there-a-way-to-force-javascript-to-return-a-negative-value-in-an-alert-box%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      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();








      share|improve this answer























      • All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
        – Evan Howington
        1 hour ago

















      up vote
      2
      down vote



      accepted










      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();








      share|improve this answer























      • All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
        – Evan Howington
        1 hour ago















      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      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();








      share|improve this answer














      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();








      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();





      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();






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 1 hour ago

























      answered 1 hour ago









      Abana Clara

      1,487719




      1,487719












      • All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
        – Evan Howington
        1 hour ago




















      • All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
        – Evan Howington
        1 hour ago


















      All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
      – Evan Howington
      1 hour ago






      All good answers. Thank you for showing the Number part in the code. I had changed it to initial_velocity - ((-1)*acceleration*time) and that worked.. I changed to that because one of my other equations is almost identical except it utilizes subtraction and there were no issues. Previously I was just performing multiplication and division so this did not arise. I am converting my code from Python to JS, so I am learning on the fly. Thank you very much!
      – Evan Howington
      1 hour ago














      up vote
      6
      down vote













      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');





      share|improve this answer



























        up vote
        6
        down vote













        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');





        share|improve this answer

























          up vote
          6
          down vote










          up vote
          6
          down vote









          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');





          share|improve this answer














          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');






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Alexander O'Mara

          42.9k1395128




          42.9k1395128






















              up vote
              1
              down vote













              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.






              share|improve this answer

























                up vote
                1
                down vote













                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.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  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.






                  share|improve this answer












                  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.






                  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());





                  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());






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Feathercrown

                  1,61611021




                  1,61611021






















                      Evan Howington is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Evan Howington is a new contributor. Be nice, and check out our Code of Conduct.













                      Evan Howington is a new contributor. Be nice, and check out our Code of Conduct.












                      Evan Howington is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53862185%2fis-there-a-way-to-force-javascript-to-return-a-negative-value-in-an-alert-box%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      數位音樂下載

                      格利澤436b

                      When can things happen in Etherscan, such as the picture below?