Loops and Arrays in JavaScript

Slides:



Advertisements
Similar presentations
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
PHP Conditions MIS 3501, Fall 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University September 11, 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript, Sixth Edition
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
CSC 211 Java I for loops and arrays.
Chapter 11 - JavaScript: Arrays
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
EasyCode Foundations Vocabulary Terms.
Loops BIS1523 – Lecture 10.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
PHP: includes MIS 3501 Jeremy Shafer Department of MIS
Class07 PHP: loops and includes
Programming the Web using XHTML and JavaScript
JavaScript Syntax and Semantics
JavaScript: Functions.
The structure of computer programs
Siti Nurbaya Ismail Senior Lecturer
While Loops BIS1523 – Lecture 12.
Introduction to AJAX MIS 3502 Jeremy Shafer Department of MIS
Introduction to JavaScript
Introduction to AJAX MIS 3502 Jeremy Shafer Department of MIS
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
PDO Database Connections: Getting data out of the database
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
A second look at JavaScript
A (gentle???) Introduction to JavaScript
Java Programming Arrays
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
T. Jumana Abu Shmais – AOU - Riyadh
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Introduction To Programming Information Technology , 1’st Semester
Javascript Arrays.
MSIS 655 Advanced Business Applications Programming
3.1 Iteration Loops For … To … Next 18/01/2019.
An Introduction to JavaScript
Programming Control Structures with JavaScript Part 2
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Arrays ICS2O.
An introduction to jQuery
Conditional Statements with JavaScript
JavaScript: Arrays.
JavaScript objects, functions, and events
Arrays in Java.
Programming Control Structures
Introduction to JavaScript
Vocabulary Memory Cards--Sample
PDO and Arrays MIS 3502 Jeremy Shafer Department of MIS
Programming Control Structures with JavaScript
CSC1401 Manipulating Pictures 2
Strings and Dates in JavaScript
Web Programming and Design
Lecture 9: JavaScript Syntax
Week 7 - Monday CS 121.
Presentation transcript:

Loops and Arrays in JavaScript MIS 2402 Jeremy Shafer Department of MIS Fox School of Business Temple University

Agenda Syntax for writing “for” loops in JavaScript Introduction to arrays Some practical examples of loops

What’s a loop? In programming, a “loop” is a series of commands (called “a block of code”) that repeats for a specified number of iterations. It’s a programmatic way of doing the same thing over and over again.

The syntax of a JavaScript for loop For example: Write the numbers 1 to 100 to the console. for (var x = 1; x <= 100; x++) { console.log(x); } The loop continues while this expression is true. x is declared and assigned a value. A shorthand way of saying x = x + 1 The code block begins and ends.

Another (simple) example of a for loop Thinking ahead! We establish sumOfNumbers here so we can use it later in the loop. We’re using a variable to control the number of iteration.

Another example of a for loop Let’s see a loop at work! (see rocket_06.html in rocket2.zip)

What’s 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.

The syntax for creating an array

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.

How big is the array? QUESTION: Using the totals array from the last slide, what number would this command display? console.log(totals.length);

Putting arrays to work

Putting arrays to work(2) QUESTION: What one line of code here could we add after “what’s next” that would write the sum of totals to the console? What about the average? Recalling last week’s challenge, what else could we do to report the result of our calculation? (see rocket_07.html)

So… what are loops good for? Performing a calculation that requires iterations. Stepping though each element of an array. Generating repetitive HTML. <table> rows, <select> options, <ul> items, <ol> items. Any combination of the above! (see rocket_08.html)

Next time…