| Index | Code Tester |
var x = 10 var y = 2 var z = 3 var answer answer =x + y //12 answer = x*y //20 answer = x/y //5 answer = x%z //1 answer = 10*(x+y)/z //40 answer = 3.2*2 //6.4
Unary Operators
var x = 0 x++ //x =1 x= x+1 //x = 2 x-- //x = 1 var y y = ++x //y=2 and x =2 This increments x then assign x to y y = x++ //y=2 and x = 3 This assigns x to y then increments x
1. What happens if you use the above operators on strings and booleans?
2. Visit www.w3schools.com and find their discussion of assignment operators. List the operators and their equivalent expressions.
3. Create a simple calculator page. Your page should have three textboxes and five radio buttons. Each radio button should represent one of the operators +, -, *, /, %. When the user clicks one of the radio buttons the contents of the first two textboxes should be combined using the selected operator and the result displayed in the third textbox.
4. Create a page with a global variable called gNumClicks. Create a function called incrementClicks(). This function should use the increment operator to increment the variable gNumClicks. Every time the user clicks on the page, the function should be called and the value of gNumClicks should be displayed in an alert. What happens if you use document.write to display gNumClicks? What happens if you do not initialize gNumClicks? Write the answers to these questions on your page.
5. In #4, what happens if gNumClicks is declared in the function instead of globally?
6. What will be displayed in the alerts?
a. What operators are performed first?
b. How can we avoid confusion with complicated expressions?
c. Why does one of these alert's display "infinity"?
7. Create a page to demonstrate the difference between x++ and ++x.