Understanding Arrays, Looping, sequence and selection structures

Javascript structure homepage image

Instructor: Chris Fulton

Learning Objectives

  • Arrays
    • What is an Array | Array Syntax
    • Array Use Case Scenarios
      • Storing Single dimension data | Storing data in a program | Select Options List
    • Processing an Array
      • Array Helper Methods - forEach | map | reduce | filter | every | some
    • Array Helper Methods
  • Flow Control
    • Looping/Iteration
      • while | do/while | for statements
    • Decision Making Logic
      • if statements | if/else statements | switch statements | ternary
    • JavaScript Ayschronous Functionality

JS Arrays

What is an Array?

Definition
Set of data represented by a single variable name

Index
the position or location within the array

Element/Value
the data stored within the position of the array

Array Literal
most common way to create an array
declares a variable and specifies array as content

Array Object
the syntax for declaring a new array instance

Array Syntax

arrow function example image
  • Arrow functions allows a short syntax for writing function expressions. You don't need the function keyword, the return keyword, and the curly brackets.
  • You can only omit the return keyword and the curly brackets if the function is a single statement.

Array Helper Methods

array helper methods

The map, reduce, filter methods allow for a variety of ways to work with the data contained in the list.

Control Structures

The Three Structures

Sequence ✦ Selection ✦ Looping

  • Sequence
    • Perform actions or tasks in order
    • No branching or skipping any task
    • Input/Output, Calculations, assignment
  • Selection
    • Ask a question, take one of two actions
    • Often called if-then-else
    • Dual-alternativeifsor single-alternativeifs
  • Looping
    • Repeat actions while a condition remains true

Decision Structures

if statements ⛄ if/else statements ⛄ switch statements

								
									int numOne = 2; int numTwo = 5;
									
									if(numOne >= numTwo){
									   console.log("2 is greater than 5");
									}else{
									   console.log("2 is not greater than 5");
									}
	
								
							

Dual-alternative selection structure

								
									var numOne = 2; var numTwo = 5;
									
									if(numOne <= numTwo){
								       console.log("5 is greater than 2");
									}
	
								
							

Single-alternative selection structure

Looping/Iteration

  • Loop: a structure that repeats actions while some condition continues
    • As long as a condition(s) remains true, the statements in a while loop’s body execute
  • Use Cases:
    • Validating Data by Defensive Programming | Outputing Structure Data | Reprompting | Repetitive Output(Such as counting, using math, or general text ouput)

Three statements needed for Looping

Initial loop control variable initialized before entering loop

Loop control variable tested

Body of loop must alter value of loop control variable

								
										var initialValue = 0;  //Initial Value for Control variable
										
										var endValue = 4;
										
										var topics = [ "Structures", "Decision Making", "Looping", "Arrays"];
										
										while(initialValue < endValue){    //Control variable tested
										
									    	console.log("Week " + (initialValue + 1) + " is " + topics[initialValue]);
									      
									    	initialValue++; //Control Variable altered
									       
										}
										
										console.log("Finished");
									 
									 
	
								
							

Activity 6

2014 calendar graphic

Step 1
Observe the diagram above and declare for the data you will use in program. Declare an array that holds the month names.

Step 2
Think through and apply the correct structures to fulfill the requirements of the program.

Step 3
Code the program and output the month along with the correct number of days for each month.

Video Lectures

Click here for playlist

References

  • https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS