Download presentation
Presentation is loading. Please wait.
Published byIda Makmur Modified over 5 years ago
1
Programming Control Structures with JavaScript Part 2
MIS 2402 Jeremy Shafer Department of MIS Fox School of Business Temple University
2
Let’s talk about… Chapter 3
What are the three kinds of loops described in chapter 3? What is a loop good for? What are some physical things could you represent with an array?
3
Agenda Syntax for writing loops in JavaScript Introduction to arrays
The “while” loop The “do while” loop The “for” loop Introduction to arrays
4
The syntax of a while loop
5
The syntax of a do-while loop
6
Another do-while loop example
DISCUSS – what, in your own words, is this code saying? If you wanted to describe these 9 lines of code with a comment, what would the comment be?
7
The syntax of a for loop
8
Another example of a for loop
Let’s see some loops at work! (see rocket.zip)
9
What is an array? An array is a data type that can contain one or more items called elements. Each element stores a value that you can refer to with an index. The length of the array indicates the number of elements that it contains. In short, an array is a structure that allows us to store multiple pieces of similar data.
10
The syntax for creating an array
11
How to add values to an array
This number, inside the square brackets, is the index to the array. It indicates what piece of the array we are going to add a value to. QUESTION: What is the index of the first value in the above array? Careful! JavaScript arrays are zero-based and that is often a source of trouble for first-time coders. Now that the totals array exist, and some values have been assigned to it, I can use totals[2], totals[1] and totals[0] like any other primitive variable.
12
How big is the array? QUESTION: Using the totals array from the last slide, what number would this command display? alert(totals.length);
13
Putting arrays to work
14
Putting arrays to work(2)
QUESTION: Using the JavaScript covered in this lecture, what one line of code here could you add after “what’s next” that would alert the user and display the average of totals?
16
Next time… Next time we meet we will work through exercises 3-1 and 3-2 in your textbook. We’ll get more practice with loops and arrays!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.