Advanced Web Programming - Chapter 9: Arrays
29 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition

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']</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</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</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</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</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'</p> Signup and view all the answers

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

    <p>shift</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</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</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']</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.</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</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.</p> Signup and view all the answers

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

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

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

    <p>6.28318</p> Signup and view all the answers

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

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

    How can you represent negative numbers in JavaScript?

    <p>By adding a negative sign before the number.</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</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</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</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</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</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</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</p> Signup and view all the answers

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

    <p>concat</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</p> 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"]

    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

    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.

    More Like This

    Use Quizgecko on...
    Browser
    Browser