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?
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?
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?
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?
Signup and view all the answers
In the context of arrays, what does the index value represent?
In the context of arrays, what does the index value represent?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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"));?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What is the main function of the pop method in array manipulation?
What is the main function of the pop method in array manipulation?
Signup and view all the answers
What is the result of the expression good.concat(bad)
?
What is the result of the expression good.concat(bad)
?
Signup and view all the answers
Which of the following statements about the variable stooges
is true?
Which of the following statements about the variable stooges
is true?
Signup and view all the answers
Which of the following shows the correct order of operations in JavaScript?
Which of the following shows the correct order of operations in JavaScript?
Signup and view all the answers
Which operation does the expression var i = i / 2;
perform?
Which operation does the expression var i = i / 2;
perform?
Signup and view all the answers
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?
Signup and view all the answers
What will be the result of executing var doublePi = 2 * 3.14159;
?
What will be the result of executing var doublePi = 2 * 3.14159;
?
Signup and view all the answers
What does the expression i = i + 1;
achieve?
What does the expression i = i + 1;
achieve?
Signup and view all the answers
How can you represent negative numbers in JavaScript?
How can you represent negative numbers in JavaScript?
Signup and view all the answers
What is the primary function of the shift method in an array?
What is the primary function of the shift method in an array?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
How does the lastIndexOf method operate compared to the indexOf method?
How does the lastIndexOf method operate compared to the indexOf method?
Signup and view all the answers
What does the slice method do in relation to an array?
What does the slice method do in relation to an array?
Signup and view all the answers
What is returned by calling the unshift method on an array?
What is returned by calling the unshift method on an array?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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);
Signup and view all the answers
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.
Related Documents
Description
This quiz covers key concepts from Chapter 9 of Advanced Web Programming, focusing on arrays and numerical values. You will learn how to create, access, and manipulate arrays while exploring the Math
object. Test your understanding of array properties and their significance in handling lists of data.