Presentation is loading. Please wait.

Presentation is loading. Please wait.

What happens when this code executes?

Similar presentations


Presentation on theme: "What happens when this code executes?"— Presentation transcript:

1 What happens when this code executes?
var x; var y=3; x=y*5; alert(x); What happens when this code executes? We get an error; x isn’t declared We get an error; alert isn’t defined 15 is shown in a dialog box 15 is printed to the webpage “x” is shown in a dialog box “x” is printed to the webpage

2 What happens when this code executes?
var x; var y=3; x=y*5; alert(x); What happens when this code executes? We get an error; x isn’t declared We get an error; alert isn’t defined 15 is shown in a dialog box 15 is printed to the webpage “x” is shown in a dialog box “x” is printed to the webpage

3 What happens when this code executes?
var x=5; var z=x; x=“stuff”; document.write(x); What happens when this code executes? We get an error; x can’t hold a number and then “stuff” We get an error; document.write() doesn’t take parameters “stuff” is printed to the webpage 5 is printed to the webpage

4 What happens when this code executes?
var x=5; var z=x; x=“stuff”; document.write(x); What happens when this code executes? We get an error; x can’t hold a number and then “stuff” We get an error; document.write() doesn’t take parameters “stuff” is printed to the webpage 5 is printed to the webpage

5 What value will z hold when this finishes?
var x=5; var y=6; z=x*y; z=z*z; What value will z hold when this finishes? We get an error; z*z can’t be assigned to z We get an error; z wasn’t declared 900 27000

6 What value will z hold when this finishes?
var x=5; var y=6; z=x*y; z=z*z; What value will z hold when this finishes? We get an error; z*z can’t be assigned to z We get an error; z wasn’t declared 900 27000

7 What value will x hold when this finishes?
var x=3; if(x>4) { x=x+5; } else x=x*2; What value will x hold when this finishes? We get an error; ‘else’ isn’t declared 8 6 16

8 What value will x hold when this finishes?
var x=3; if(x>4) { x=x+5; } else x=x*2; What value will x hold when this finishes? We get an error; ‘else’ isn’t declared 8 6 16

9 What gets shown in a dialog box when this function is called?
function fun() var x=3; var y=4; alert(x*y); What gets shown in a dialog box when this function is called? Nothing; an error occurs because the function is missing ‘{‘ & ‘}’ Nothing; an error occurs because variables cannot be declared in a function Nothing; an error occurs because this function is missing parameters 12

10 What gets shown in a dialog box when this function is called?
function fun() var x=3; var y=4; alert(x*y); What gets shown in a dialog box when this function is called? Nothing; an error occurs because the function is missing ‘{‘ & ‘}’ Nothing; an error occurs because variables cannot be declared in a function Nothing; an error occurs because this function is missing parameters 12

11 var x=0,z; for(z=10;z>7;z--) { x=x+1; } What is the value of x when this loop finishes (how many times does the code in the loop run)? 3 4 5 6

12 var x=0,z; for(z=10;z>7;z--) { x=x+1; } What is the value of x when this loop finishes (how many times does the code in the loop run)? 3 4 5 6

13 var x=0,z; for(z=43;z>28;z++) { x=x+1; } What is the value of x when this loop finishes (how many times does the code in the loop run)? 14 15 16 Infinite loop

14 var x=0,z; for(z=43;z>28;z++) { x=x+1; } What is the value of x when this loop finishes (how many times does the code in the loop run)? 14 15 16 Infinite loop

15 What is the value of y when this loop finishes?
var x=0, y=1,z,tmp; for(z=2;z<4;z++) { tmp=x+y; x=y; y=tmp; } What is the value of y when this loop finishes? 1 2 3 4 5

16 What is the value of y when this loop finishes?
var x=0, y=1,z,tmp; for(z=2;z<4;z++) { tmp=x+y; x=y; y=tmp; } What is the value of y when this loop finishes? 1 2 3 4 5

17 Write a javascript function called showValue() that reads the value from this text-field in the html, and shows it in a dialog box: <input type=“text” id=“txtbox”>

18 Write a javascript function called showValue() that reads the value from this text-field in the html, and shows it in a dialog box: <input type=“text” id=“txtbox”> function showValue() { var x=document.getElementById("txtbox"); alert(x.value); }

19 Given an array named ‘arr’, write javascript code that checks to see if the size of the array is at least 3; if not, it displays “Error” in a dialog box. If so, show, in a dialog box, the sum of the first 3 elements (assume they’re numbers).

20 Given an array named ‘arr’, write javascript code that checks to see if the size of the array is at least 3; if not, it displays “Error” in a dialog box. If so, show, in a dialog box, the sum of the first 3 elements (assume they’re numbers). If(arr.length<3) { alert("Error"); } else alert(arr[0]+arr[1]+arr[2]);

21 Given an array named ‘arr’, write javascript code that displays, in a dialog box, the sum of all its elements (assume they’re all numbers).

22 Given an array named ‘arr’, write javascript code that displays, in a dialog box, the sum of all its elements (assume they’re all numbers). var i; var sum=0; for(i=0;i<arr.length;i++) { sum+=arr[i]; } alert(sum);


Download ppt "What happens when this code executes?"

Similar presentations


Ads by Google