Download presentation
Presentation is loading. Please wait.
Published byJordan Holland Modified over 8 years ago
2
Random Student Numbers 1: 2: 3: 4: 5: Generate random student numbers: 50000001..50009999 Random Student Numbers 1: 50005837 2: 50003222 3: 50007741 4: 50004075 5: 50009410 Generate random student numb.. Random Student Numbers 1: 2: 3: 4: 5: Generate random student numbers: 50000001..50009999 The following page contains a pseudo-URL that calls the genStudentNumbers function to generate 5 random student numbers and show them in 5 given spans. //Generate random student numbers function genStudentNumbers() { } Task 2: The function below will be added to the previous code. Read the given comments and complete the function. /*Given the parameters p1 and p2 (the positions of 2 student numbers), swap the 2 student numbers. Eg. If p1 is 3 and p2 is 4, then the 3 rd and 4 th student numbers in the list will be swapped.*/ function swap( p1, p2 ) { } … Task 1: Complete the genStudentNumbers function. (1) Reminder : we will have test 2 in week 12 Lecture (Last day before Easter break!!) Lecture Exercise (10) CS1301 Introduction To Computer Programming (11-12 Semester B) www.cs.cityu.edu.hk/~helena
3
SORTING can be done by rearranging successive student numbers such that larger student numbers go to the bottom. To illustrate the sorting process, we extend the page so that we can click to call a fixOrder function that checks and arranges a pair of successive student numbers: For the given pair of successive student numbers, if the innerHTML of the first one is larger than the second one, swap them.. Task 3: Complete the fixOrder function. fix the order (4,5) //Compare 2 successive student numbers and fix their order: //(If the innerHTML of the first one is larger than the second one, swap them) function fixOrder(p1,p2) { } Task 4: Design a sort function that can be called to sort all 5 student numbers in one click. Advanced tasks 1.Can you write a function that can be called by onload to generates the 5 empty spans (with ids)? 2.Add code to the page so that it asks the user “how many students?” and then handle the tasks accordingly. 3.Generate unique student ids (no duplicate).
4
(3) Refer to a lecture exercise in week 9 - [Given Code] LectEx09_ShowPrimeNumbers.html If we remove both “var” for i, then the new window shows ‘3’ repeatedly (can’t stop). Why? (2) (i) Explain the code below. (ii) Do we need to add javascript: ? (iii) Do we need to add void(0); ? <span onmouseover="document.F1.n1.value=8;" onmouseout="document.getElementById('t1').value=9;" > move around!!
5
If we remove both “var” for i, then the new window shows ‘3’ repeatedly (can’t stop). Why? HINT / DISCUSSION: i used in showPrimeNumbers and isPrime should be different variables. (I) If both “var” are removed. Then “i=start;” (line 21) will automatically add a property “i” of the window object ( window.i ) (This will be automatically done because i has not been declared as a variable in the function). Later, at lines 8-12, “i=2” will actually refer to window.i. (The same as lines 22-25). That is, both isPrime() and showPrimeNumbers() share the same i !! Javascript is convenient (flexible, less constraints, loose in syntax), BUT easy to create problem (easily go wrong, bad style / habit). So, actually, Javascript programmers need to be serious about what he is writing. Traditional languages like Pascal, C++: Tight but safe to create proper code!! (Easier to debug!!) – eg. must add semi-colon, variables must be declared. (II)[ Interested students only!! ] But why show ‘3’ repeatedly? Answer: Line 23: Check whether window.i (= 50) is prime ==> false ==> won’t write (ie. won’t run line 24) But isPrime() has changed window.i has to 2 Line 25: i++; will update window.i to 3.. 3..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.