To write your own JavaScript functions.
Web Programming book reading for THIS class
while
Loopdo
Loopfor
Loop (revisited)Web Programming book reading for NEXT class
How to print large number with commas, use the built-in function toLocaleString("en-US")
var n = 12345 ** 2 ; // result is 152399025 // ** is the exponent operator, so ** 2 squares the number document.writeln( n.toLocaleString("en-US") ) ; // displays 152,399,025 // note that the number is displayed as a STRING
document.writeln( n.toLocaleString() ) ; // also displays 152,399,025 // but it only displays this way if the user in in the United States // WHY IS THIS SO?
Link to original article: Format Numbers with Commas in JavaScript
Questions from our last class or the readings or work between classes?
for
Loop
source: Connolly & Hoar, Fundamentals of Web Development, fig. 8.11, p. 373
k++
is the same ask = k + 1
- pre-increment means that the addition is done before
k
is used- post-increment means that the addition is done after
k
is usedExample:
for ( var k = 0 ; k < 5 ; k++ ) { document.writeln( k + " " ) ; // output: 0 1 2 3 4 }
while
Loop
source: Dean, Web Programming, fig. 10-01, p. 427in this case,
- we have to specify the initialization and continuation condition as distinct statements
- in addition, we have to change some part of the continuation condition inside the loop or we will have an infinite loop that never terminates
Example:
var k = 0 ; // initialization, which is outside the loop while ( k < 5 ) { // the continuation condition goes here document.writeln( k + " " ) ; // output: 0 1 2 3 4 k++ ; // be sure to change some part of the continuation condition }Note that a
while
loop might execute 0 times if the continuation condition is already satisfied when the loop is entered.
- for example, imagine that
k
is6
(instead of 0) when thewhile
statement is reached in the above example- in that case, the continuation condition would be
false
, and the statements inside the loop would simply be skipped
do
Loop
source: Dean, Web Programming, fig. 10-6, p. 437The only difference between a
do
loop and awhile
loop is where the continuation condition is.in this case,
- the continuation condition is at the bottom of the loop
- the important consequence of this positioning is that a do loop will always execute at least once
Example:
var k = 0 ; // initialization, which is outside the loop do { document.writeln( k + " " ) ; // output: 0 1 2 3 4 k++ ; // be sure to change some part of the continuation condition } while ( k < 5 ) ; // the continuation condition goes here
for
Loop Revisited
source: Dean, Web Programming, fig. 10-13, p. 452
Preamble: This exercise is the same as the one for our last
class to give us more time to work with loops on the computers. If you have
completed or nearly completed this exercise and feel comfortable with its concepts,
modify your code to use the getElementById
technique introduced on page 315
of our texbook and used throughout Chapter 8.
Goal: To create a dynamic table.
Procedure:
JavaScript Multiplication Table (Heines)
0 1 2 3 4 5 6 7 8 9
0 10 20 30 40 50 60 70 80 90
<table>
and </table>
tags in standard HTML. for
loop to output 0-9 a single line of the desired
table. These numbers will be the first numbers to be multiplied.
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
for
loop to print the subsequent lines and
the results of the multiplication.
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 10 12 14 16 18
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 2 0 2 4 6 8 10 12 14 16 18
http://cssdweb.edu/username/filename.html
This is Class No. 15. It was last modified on
Saturday, October 21, 2023 at 4:38 PM. Copyright © 2010-2024 by Jesse M. Heines. All rights reserved, but may be freely copied or excerpted for
educational purposes with credit to the author.