Presentation is loading. Please wait.

Presentation is loading. Please wait.

Add 2 Numbers using a Function Input Number 1 Input Number 2 Call a function that adds these numbers Write the answer out to screen.

Similar presentations


Presentation on theme: "Add 2 Numbers using a Function Input Number 1 Input Number 2 Call a function that adds these numbers Write the answer out to screen."— Presentation transcript:

1 Add 2 Numbers using a Function Input Number 1 Input Number 2 Call a function that adds these numbers Write the answer out to screen

2 Step 1: Put in tags

3 Step 2: Input 2 Numbers number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0);

4 Step 3: Create a function to add 2 numbers function Add2Numbers(number1, number2) { total=number1+number2; } number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0);

5 Step 4: Call the Function function Add2Numbers(number1, number2) { total=number1+number2; } number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0); Add2Numbers(number1, number2);

6 Step 5: Write the answer out to screen function Add2Numbers(number1, number2) { total=number1+number2; } number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0); Add2Numbers(number1, number2); document.write("The sum of the numbers you entered is: "+total);

7 Step 5: Write the answer out to screen total=0; function Add2Numbers(number1, number2) { total=number1+number2; } number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0); Add2Numbers(number1, number2); document.write("The sum of the numbers you entered is: "+total); Problem!!! The output of this program is (with input 1,2): The sum of the numbers you entered is: 12 12 is the wrong answer, it should be 3 How do we fix this??

8 Step 6: Fix the problem total=0; function Add2Numbers(number1, number2) { total=number1+number2; } number1= prompt("Enter 1st number:", 0); number2= prompt("Enter 2nd number:", 0); number1=parseInt(number1); number2=parseInt(number2); Add2Numbers(number1, number2); document.write("The sum of the numbers you entered is: "+total);


Download ppt "Add 2 Numbers using a Function Input Number 1 Input Number 2 Call a function that adds these numbers Write the answer out to screen."

Similar presentations


Ads by Google