Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Supp (11) Week 10 lecture exercise [Given Code] LectEx10_ShowPrimeNumbers.html If we remove both “var” for i, then the new window shows ‘3’ repeatedly.

Similar presentations


Presentation on theme: "Lecture Supp (11) Week 10 lecture exercise [Given Code] LectEx10_ShowPrimeNumbers.html If we remove both “var” for i, then the new window shows ‘3’ repeatedly."— Presentation transcript:

1

2 Lecture Supp (11) Week 10 lecture exercise [Given Code] LectEx10_ShowPrimeNumbers.html If we remove both “var” for i, then the new window shows ‘3’ repeatedly (can’t stop). Why? CS1301 Introduction To Computer Programming (07-08 Semester B) www.cs.cityu.edu.hk/~helena P. 1

3 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. (1) 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 or anything). 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. (2)[ 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.. P. 2


Download ppt "Lecture Supp (11) Week 10 lecture exercise [Given Code] LectEx10_ShowPrimeNumbers.html If we remove both “var” for i, then the new window shows ‘3’ repeatedly."

Similar presentations


Ads by Google