Page 1 Lecture Exercise (Wk4) CS1301 Introduction To Computer Programming (07-08 Semester B) Question 2 According to the following code, what will be shown in the page? Try calling functions function x1() {return 3+x2(8); } function x2(value) {return value*5; } document.write(x1()); calculate 3 + x2(8) and then return the result (i)var x, a, b; a = 4; b = 4*8; x = a+b; a = b; b = a; (ii)var a,b,x; a=1; b=2; x=3; 4=a; a+b=x; Question 1 Explain what are done (or error) by the code: Question 3 (a)Complete the following code to check whether a value x is odd or not. (Hint: use %) var x=prompt("Input a value for x"); if (________________) alert("It is even.",""); else alert("It is odd.");
Page 2 (b)We want to increase the salaries of both junior and senior staff by 10percents, but rounded up by 10 dollars. For example, Original salary is: 7730 After raising by 10percents => 8503 Then round up by 10 dollars => 8510 Design a function that can be called to do the calculation. (A solution: If raised - salary % 10 is equal to 0, no need to adjust. Otherwise calculate the amount to add.) Question 4 We usually add comments to explain our code. Particularly, we add comments (1) for all global variables (2) to describe functions (add simple comments before each function) (3) to briefly explain different parts of the code (4) for some complicated logic whose meaning / purpose is not obvious In formal practices, we also add comments at the beginning of the code to show (1) brief description of the application (2) name of the person who created / modified the code (3) creation / modification dates (4) any special notes for the user Add appropriate comments to the code: CS1301 DEMO var c=0; function count( ) {c=c+1; document.getElementById("counter").innerHTML=c; } Add 1 to the counter :