Download presentation
Presentation is loading. Please wait.
2
Lecture Exercise (9) CS1301 Introduction To Computer Programming (07-08 Semester B) www.cs.cityu.edu.hk/~helena Random Student Numbers 1: 2: 3: 4: 5: Generate random student numbers: 50000001..50009999 Random Student Numb 1: 50005837 2: 50003222 3: 50007741 4: 50004075 5: 50009410 Generate random studen 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.
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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.