Correcting my Javascript math program?

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" "">

  <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 


Initiative Q

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.


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

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.

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

@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…