Advanced Web Programming - Chapter 9: Arrays

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 5
  • 6
  • 4 (correct)
  • 0

Which of the following options correctly demonstrates array literals with non-empty values?

<p>var groceries = ['Milk', 'Eggs', 'Juice'] (B)</p> Signup and view all the answers

In the context of arrays, what does the index value represent?

<p>The position of the item within the array (C)</p> 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?

<p>It returns undefined (A)</p> Signup and view all the answers

What is the correct behavior of the push method when used on an array?

<p>It adds an item to the end of the array and returns the new length (A)</p> Signup and view all the answers

What happens to the index values of an array when you use the unshift method?

<p>They increase to account for the new item added at the beginning (D)</p> Signup and view all the answers

What is the expected result of the alert statement after this code: alert(groceries.push("Cookies"));?

<p>It alerts the total number of items in the array after adding 'Cookies' (D)</p> Signup and view all the answers

Which method would you use to remove the first item from an array?

<p>shift (D)</p> Signup and view all the answers

If an array's length property returns 6, what does this indicate about the array?

<p>It can hold up to 6 items but may have less (B)</p> Signup and view all the answers

What is the main function of the pop method in array manipulation?

<p>To remove and return the last item from the array (A)</p> Signup and view all the answers

What is the result of the expression good.concat(bad)?

<p>['Mario', 'Luigi', 'Kirby', 'Yoshi', 'Bowser', 'Koopa Troopa', 'Goomba'] (B)</p> Signup and view all the answers

Which of the following statements about the variable stooges is true?

<p>It is a variable that contains an integer value. (D)</p> Signup and view all the answers

Which of the following shows the correct order of operations in JavaScript?

<p>Parenthesis, Exponents, Multiply, Divide, Add, Subtract (D)</p> Signup and view all the answers

Which operation does the expression var i = i / 2; perform?

<p>It divides the current value of i by 2. (A)</p> Signup and view all the answers

If temperature is initialized as var temperature = −42; what type of number is it?

<p>Integer number (D)</p> Signup and view all the answers

What will be the result of executing var doublePi = 2 * 3.14159;?

<p>6.28318 (A)</p> Signup and view all the answers

What does the expression i = i + 1; achieve?

<p>It increments the variable i by 1. (D)</p> Signup and view all the answers

How can you represent negative numbers in JavaScript?

<p>By adding a negative sign before the number. (A)</p> Signup and view all the answers

What is the primary function of the shift method in an array?

<p>Removes and returns the first item of the array (C)</p> Signup and view all the answers

What value does the pop method return when an item is removed from an array?

<p>The removed item itself (B)</p> Signup and view all the answers

When using the indexOf method, what does it return if the item is not found in the array?

<p>−1 (B)</p> Signup and view all the answers

How does the lastIndexOf method operate compared to the indexOf method?

<p>It finds the last occurrence of an item (D)</p> Signup and view all the answers

What does the slice method do in relation to an array?

<p>Copies a portion of the array and returns a new array (C)</p> Signup and view all the answers

What is returned by calling the unshift method on an array?

<p>The new length of the array (B)</p> Signup and view all the answers

When an item is shifted from an array, what happens to the remaining elements' index positions?

<p>They are decremented by 1 (D)</p> Signup and view all the answers

Which method would you use to merge two different arrays into a new array?

<p>concat (B)</p> 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);

<p>1 (A)</p> Signup and view all the answers

Flashcards

Array

An ordered list of values, each with a unique index (starting from 0).

Array index

A numerical position assigned to each item in an array, starting with 0.

Array literal notation

A way to create an array using square brackets.

Accessing array values

Retrieving a specific item from an array using its index.

Signup and view all the flashcards

Array element

A single item within an array.

Signup and view all the flashcards

Array length

The number of items in an array.

Signup and view all the flashcards

Accessing array elements

Array elements are accessed using their index, which starts at 0.

Signup and view all the flashcards

Array length

The length property of an array gives the number of items in it.

Signup and view all the flashcards

Looping through an array

A for loop is used to iterate over all items in an array.

Signup and view all the flashcards

Adding items to end (push)

The push() method adds an item to the end of an array.

Signup and view all the flashcards

Adding items to beginning (unshift)

The unshift() method adds an item to the start of an array.

Signup and view all the flashcards

Removing items from the end (pop)

The pop() method removes and returns the last item from an array

Signup and view all the flashcards

Removing items from start (shift)

The shift() method removes and returns the first item from an array.

Signup and view all the flashcards

Array shift method

Removes and returns the first item from an array. The remaining indexes are adjusted.

Signup and view all the flashcards

Array unshift method

Adds new items to the beginning of an array and returns the new length of the array

Signup and view all the flashcards

Array push method

Adds new items to the end of an array and returns a new length of the array

Signup and view all the flashcards

Array pop method

Removes and returns the last item from an array. The remaining indexes are adjusted.

Signup and view all the flashcards

Array slice method

Copies a portion of an array into a new array without modifying the original.

Signup and view all the flashcards

Array indexOf method

Returns the index of the first occurrence of a given value in an array.

Signup and view all the flashcards

Array lastIndexOf method

Returns the index of the last occurrence of a given value in an array.

Signup and view all the flashcards

indexOf return value if not found

-1

Signup and view all the flashcards

lastIndexOf return value if not found

-1

Signup and view all the flashcards

Merging Arrays

Combining two or more arrays into a new array.

Signup and view all the flashcards

Array Concatenation

Combining two arrays into a single array using the .concat() method.

Signup and view all the flashcards

JavaScript Arrays

Ordered collections of data (e.g., strings, numbers).

Signup and view all the flashcards

.concat() Method

Adds the elements of one array to the end of another, creating a new array.

Signup and view all the flashcards

JavaScript Numbers

Data type representing numerical values, including integers, decimals, and scientific notation.

Signup and view all the flashcards

Variable Initialization

Giving a value to a variable for the first time.

Signup and view all the flashcards

Arithmetic Operators

  • (addition), - (subtraction), * (multiplication), / (division), % (modulo).
Signup and view all the flashcards

Operator Precedence

Rules determining the order of operations in an expression (e.g., parentheses first).

Signup and view all the flashcards

Incrementing a variable

Increasing the value of a variable by adding a number, often 1.

Signup and view all the flashcards

Decrementing a variable

Decreasing the value of a variable by subtracting a number, often 1.

Signup and view all the flashcards

Cumulative Modification

Repeatedly changing the value of a variable (e.g., using +=, -=).

Signup and view all the flashcards

Complex Numbers in Javascript

Numbers with a real component and an imaginary component (this document does not show imaginary numbers).

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"]

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++: Increments i by 1.
  • i--: Decrements i by 1.
  • i += n: Increments i by n.
  • i -= n: Decrements i by n.

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.

Quiz Team

Related Documents

More Like This

JavaScript Arrays Properties and Methods
10 questions
Advanced Web Programming - Chapter 9
24 questions
JavaScript Arrays
13 questions

JavaScript Arrays

MeritoriousCottonPlant3723 avatar
MeritoriousCottonPlant3723
Use Quizgecko on...
Browser
Browser