Correcting my Javascript math program?

Correcting my Javascript math program?
0

#1

Below is my Javascript math program. I’m not sure what I am doing wrong but it will not function properly. Please help!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
  <title>JavaScript Functions</title>

  <script type="text/javascript">
    var num1 = 0;//acts as a place holder for the user number 
    var num2 = 0;//acts as a place holder for the user's second number 
    var calculatedNum = 0;//place holder for the user's answer 

    function calculation() { 
      // type your calculation and output statements here 
      calculatedNum = num1 * num2; //equation for the user's two numbers
      //holds user's first number and second number, then multiplies them together 
      num1 = prompt("Please type the first number"); //prompt asking for the user's first number 
      num2 = prompt("Please type the second number"); //prompt asking for the user's second number 

      alert( num1 + " x " + num2 + " is " + calculatedNum + " ! " ); 
    } // end calculation function 
  </script>
</head>

<body>
  <h1></h1>
</body>
</html>

Shani
Initiative Q


#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#3

Shouldn’t you first do prompt then multiply numbers that were written in prompt ?


#4

num1 and num2 are both 0 when you try to calculate. Edit: ha, that won’t matter because of what @PaoloDiBello said. If you aren’t calling a function, don’t put them in a function. Otherwise, you need to have something that triggers the function.


#5

Shouldn’t you also call the function you have created?


#6

@DanCouper, you’re right :sweat_smile::sweat_smile:
At least for testing he can try to call the function to test that the function works properly…

If he plans to be using the function later on in combination with other functions…