Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1301 Introduction To Computer Programming (11-12 Semester B) www.cs.cityu.edu.hk/~helena Lecture Exercise (Wk8) Q.1 Write the code that asks the user.

Similar presentations


Presentation on theme: "CS1301 Introduction To Computer Programming (11-12 Semester B) www.cs.cityu.edu.hk/~helena Lecture Exercise (Wk8) Q.1 Write the code that asks the user."— Presentation transcript:

1

2 CS1301 Introduction To Computer Programming (11-12 Semester B) www.cs.cityu.edu.hk/~helena Lecture Exercise (Wk8) 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). Temperature Conversion (Fahrenheit to Celsius) 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() { } … Given formula: C = 5/9 * (F – 32)

3 … function listNumbers() { } … (b) Modify the above function such that each number is indicated with an “(odd)” or “(even)” label: Q. 3Complete 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). //computes the product for 1 * 2 * 3.. * n (ie. n!) function factorial(n) { } alert(factorial(4));

4 Q. 4 Complete the countFactors function in the following code according to the given comment. //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; } <input type="button" value=“ count the factors of a number " onclick="var num=parseInt( prompt(‘Input..’,..) ); alert(num+' has '+countFactors(num)+' factor(s).'); " /> 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.'); " />

5 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 + ' '; 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(); } … … - 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?


Download ppt "CS1301 Introduction To Computer Programming (11-12 Semester B) www.cs.cityu.edu.hk/~helena Lecture Exercise (Wk8) Q.1 Write the code that asks the user."

Similar presentations


Ads by Google