Transcript LectEx08

Lecture Exercise (Wk8)
CS1301 Introduction To Computer Programming (11-12 Semester B)
www.cs.cityu.edu.hk/~helena
Q.1 Write the code that asks the user to input 2 temperatures in Fahrenheit,
say 95 and 105 (using the prompt dialog). Then display the table of
temperature conversions within the range (using document.write).
<body>
Temperature Conversion <br/>(Fahrenheit to Celsius)<br/>
<script type="text/javascript">
Given formula:
C = 5/9 * (F – 32)
</script>
</body>
Q.2 [Use of while-loop]
(a) Write a function that returns a string which contains all 3’s multiples from 0 to 30 (each number should be
followed by a line break):
…
function listNumbers()
{
}
…
<body onload="alert(listNumbers());">
</body>
(b) Modify the above function such that each number is indicated
with an “(odd)” or “(even)” label:
…
function listNumbers()
{
}
…
<body onload="alert(listNumbers());">
</body>
Q. 3
Complete the following function which computes the product for 1 * 2 * 3 .. * n (ie. n!).
For example, alert(factorial(4)); shows 24 (because 1*2*3*4 is 24).
<script type="text/javascript">
//computes the product for 1 * 2 * 3 .. * n (ie. n!)
function factorial(n)
{
}
alert(factorial(4));
</script>
Q. 4 Complete the countFactors function in the following code according to the given comment.
<input type="button" value=“count the factors of a number"
onclick=" var num=parseInt( prompt(‘Input..’,..) );
alert(num+' has '+countFactors(num)+' factor(s).');
" />
//Return the number of factors of an input number n (exclude 1 and n itself). Assume n > 0.
//Approach: check the numbers from 2 to n-1 and see how many can wholly divide n
function countFactors(n)
{ var count=0;
while (
{
)
}
return count;
}
Q. 5 Complete the isPrime function according to the given comment.
/* Check whether an input number, n, is a prime number or not.
Return the result as a true or false value. Assume n > 0 */
function isPrime(
{
}
Examples of prime numbers: 2, 3, 5, 7, 11, 13, ..
(Note: 1 is not prime)
)
<input type="button" value="check whether a number is prime"
onclick=“ var num=parseInt( prompt(‘Input..’,..) );
if (isPrime(num)==true)
alert(num+' is prime.');
else
alert(num+' is not prime.');
" />
Q.6 The Photo Gallery application :
Change the code to add the "Change
number of images per page” and make
it work:
//Current left most image number in the page
var currentStartImageNum=1;
//Update the page: paragraph of images
function makeHtmlOfImages()
{ var i;
var htmlOfImages='';
i=currentStartImageNum;
while (i<=currentStartImageNum+3)
{
htmlOfImages = htmlOfImages + '<img src="Image‘ + i + '.jpg" height="120"/> ';
i++;
}
document.getElementById('images').innerHTML=htmlOfImages;
}
//Show next page
function goNext()
{
if (currentStartImageNum==17)
{
alert('This is the last page');
return;
}
currentStartImageNum=currentStartImageNum+4;
makeHtmlOfImages();
}
//Show previous page
function goPrev()
{
if (currentStartImageNum==1)
{
alert('This is the first page');
return;
}
currentStartImageNum=currentStartImageNum-4;
makeHtmlOfImages();
}
…
<input type='button' value='previous page' onclick="goPrev()"/>
<input type='button' value='next page' onclick="goNext()"/>
…
- Classify the variables used above.
Global variables:
Local variables:
- Can we change all the global variables to local?
- Should we change all the local variables to global?