Podcast
Questions and Answers
What is the correct syntax for creating an empty array in JavaScript?
What is the correct syntax for creating an empty array in JavaScript?
- var groceries = {}
- var groceries = ()
- var groceries = <>
- var groceries = [] (correct)
Given the array declaration var groceries = ['Milk', 'Eggs', 'Frosted Flakes', 'Salami', 'Juice']; what will groceries[3] return?
Given the array declaration var groceries = ['Milk', 'Eggs', 'Frosted Flakes', 'Salami', 'Juice']; what will groceries[3] return?
- Frosted Flakes
- Salami (correct)
- Juice
- Eggs
If an array has a length of 5, what is the maximum valid index value that can be accessed?
If an array has a length of 5, what is the maximum valid index value that can be accessed?
- 5
- 6
- 4 (correct)
- 0
Which of the following options correctly demonstrates array literals with non-empty values?
Which of the following options correctly demonstrates array literals with non-empty values?
In the context of arrays, what does the index value represent?
In the context of arrays, what does the index value represent?
What is the result of trying to access the sixth item in an array that only has five items?
What is the result of trying to access the sixth item in an array that only has five items?
What is the correct behavior of the push method when used on an array?
What is the correct behavior of the push method when used on an array?
What happens to the index values of an array when you use the unshift method?
What happens to the index values of an array when you use the unshift method?
What is the expected result of the alert statement after this code: alert(groceries.push("Cookies"));?
What is the expected result of the alert statement after this code: alert(groceries.push("Cookies"));?
Which method would you use to remove the first item from an array?
Which method would you use to remove the first item from an array?
If an array's length property returns 6, what does this indicate about the array?
If an array's length property returns 6, what does this indicate about the array?
What is the main function of the pop method in array manipulation?
What is the main function of the pop method in array manipulation?
What is the result of the expression good.concat(bad)
?
What is the result of the expression good.concat(bad)
?
Which of the following statements about the variable stooges
is true?
Which of the following statements about the variable stooges
is true?
Which of the following shows the correct order of operations in JavaScript?
Which of the following shows the correct order of operations in JavaScript?
Which operation does the expression var i = i / 2;
perform?
Which operation does the expression var i = i / 2;
perform?
If temperature
is initialized as var temperature = −42;
what type of number is it?
If temperature
is initialized as var temperature = −42;
what type of number is it?
What will be the result of executing var doublePi = 2 * 3.14159;
?
What will be the result of executing var doublePi = 2 * 3.14159;
?
What does the expression i = i + 1;
achieve?
What does the expression i = i + 1;
achieve?
How can you represent negative numbers in JavaScript?
How can you represent negative numbers in JavaScript?
What is the primary function of the shift method in an array?
What is the primary function of the shift method in an array?
What value does the pop method return when an item is removed from an array?
What value does the pop method return when an item is removed from an array?
When using the indexOf method, what does it return if the item is not found in the array?
When using the indexOf method, what does it return if the item is not found in the array?
How does the lastIndexOf method operate compared to the indexOf method?
How does the lastIndexOf method operate compared to the indexOf method?
What does the slice method do in relation to an array?
What does the slice method do in relation to an array?
What is returned by calling the unshift method on an array?
What is returned by calling the unshift method on an array?
When an item is shifted from an array, what happens to the remaining elements' index positions?
When an item is shifted from an array, what happens to the remaining elements' index positions?
Which method would you use to merge two different arrays into a new array?
Which method would you use to merge two different arrays into a new array?
In the following code, what does the variable resultIndex
store after execution? var groceries = ["milk", "eggs", "frosted flakes", "salami", "juice"]; var resultIndex = groceries.indexOf("eggs", 0);
In the following code, what does the variable resultIndex
store after execution? var groceries = ["milk", "eggs", "frosted flakes", "salami", "juice"]; var resultIndex = groceries.indexOf("eggs", 0);
Flashcards
Array
Array
An ordered list of values, each with a unique index (starting from 0).
Array index
Array index
A numerical position assigned to each item in an array, starting with 0.
Array literal notation
Array literal notation
A way to create an array using square brackets.
Accessing array values
Accessing array values
Signup and view all the flashcards
Array element
Array element
Signup and view all the flashcards
Array length
Array length
Signup and view all the flashcards
Accessing array elements
Accessing array elements
Signup and view all the flashcards
Array length
Array length
Signup and view all the flashcards
Looping through an array
Looping through an array
Signup and view all the flashcards
Adding items to end (push)
Adding items to end (push)
Signup and view all the flashcards
Adding items to beginning (unshift)
Adding items to beginning (unshift)
Signup and view all the flashcards
Removing items from the end (pop)
Removing items from the end (pop)
Signup and view all the flashcards
Removing items from start (shift)
Removing items from start (shift)
Signup and view all the flashcards
Array shift method
Array shift method
Signup and view all the flashcards
Array unshift method
Array unshift method
Signup and view all the flashcards
Array push method
Array push method
Signup and view all the flashcards
Array pop method
Array pop method
Signup and view all the flashcards
Array slice method
Array slice method
Signup and view all the flashcards
Array indexOf method
Array indexOf method
Signup and view all the flashcards
Array lastIndexOf method
Array lastIndexOf method
Signup and view all the flashcards
indexOf return value if not found
indexOf return value if not found
Signup and view all the flashcards
lastIndexOf return value if not found
lastIndexOf return value if not found
Signup and view all the flashcards
Merging Arrays
Merging Arrays
Signup and view all the flashcards
Array Concatenation
Array Concatenation
Signup and view all the flashcards
JavaScript Arrays
JavaScript Arrays
Signup and view all the flashcards
.concat() Method
.concat() Method
Signup and view all the flashcards
JavaScript Numbers
JavaScript Numbers
Signup and view all the flashcards
Variable Initialization
Variable Initialization
Signup and view all the flashcards
Arithmetic Operators
Arithmetic Operators
Signup and view all the flashcards
Operator Precedence
Operator Precedence
Signup and view all the flashcards
Incrementing a variable
Incrementing a variable
Signup and view all the flashcards
Decrementing a variable
Decrementing a variable
Signup and view all the flashcards
Cumulative Modification
Cumulative Modification
Signup and view all the flashcards
Complex Numbers in Javascript
Complex Numbers in Javascript
Signup and view all the flashcards
Study Notes
Advanced Web Programming - Chapter 9: Arrays, Numbers
- Arrays: Used to handle lists of data.
- Common tasks performed using array properties are discussed.
- Numerical values and the
Math
object are covered.
Arrays
- An array is a list of items, conceptually like a numbered list.
- The items in an array are called elements or array values.
- The order of elements is significant; they are indexed (starting from 0).
- Example:
- Grocery list:
["Milk", "Eggs", "Frosted Flakes", "Salami", "Juice"]
- Grocery list:
Creating an Array
- To create an array (empty):
var groceries = [];
- To create an array with values:
var groceries = ["Milk", "Eggs", "Frosted Flakes", "Salami", "Juice"];
- Values are separated by commas.
Accessing Array Values
- To access an item, use its index number inside square brackets:
groceries[1]
(accesses the second item). - Index values start from zero.
- Use
array.length
to get the number of items in an array.
Adding Items to an Array
push()
: Adds items to the end of an array. Example:groceries.push("Cookies");
unshift()
: Adds items to the beginning of an array. Example:groceries.unshift("Bananas");
- Both methods return the new array length.
Removing Items from an Array
pop()
: Removes and returns the last item.shift()
: Removes and returns the first item.
Removing Items from an Array (Cont.)
slice()
: Copies a portion of an array to a new array. Example:var newArray = groceries.slice(1. 4);
This copies elements from index 1 up to, but not including, index 4.
Finding Items in an Array
indexOf()
: Returns the index of the first occurrence of an item. Example:groceries.indexOf("Eggs");
lastIndexOf()
: Returns the index of the last occurrence of an item.
Merging Arrays
concat()
: Merges two or more arrays into a new array. Example:var goodAndBad = good.concat(bad);
good and bad are the two arrays.
Numbers (Using Numbers)
var stooges = 3;
- A simple number variable.pi = 3.14159;
- A complex number example.
Operators (Doing Simple Math)
- Basic arithmetic operators: +, -, *, /, %.
total = 4 + 26;
adds two values together.average = total / 2;
divides values
Incrementing and Decrementing
i++
: Incrementsi
by 1.i--
: Decrementsi
by 1.i += n
: Incrementsi
byn
.i -= n
: Decrementsi
byn
.
Special Values (Infinity and NaN)
Infinity
: Represents a very large number.NaN
: Represents "Not a Number" (results of invalid operations)
The Math Object
- Provides mathematical functions and constants (like
Math.PI
). - Used to perform more complex calculations and operations.
Trigonometric Functions
Math.sin()
,Math.cos()
,Math.tan()
, etc., to perform trigonometric calculations
Powers and Square Roots
Math.pow()
: Raises a number to a power (e.g.,Math.pow(2, 4)
).Math.exp()
: Raises Euler's constant (e) to a power.Math.sqrt()
: Calculates the square root.
Absolute Value
Math.abs()
: Returns the absolute value of a number. Example:Math.abs(-5)
returns 5.
Random Numbers
Math.random()
: Generates a random number between 0 (inclusive) and 1 (exclusive).
Lab
- Links to online resources for further practice (w3schools examples).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.