Advanced Web Programming - Chapter 9
44 Questions
0 Views

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 will be the result of the expression $3 / 0$ in JavaScript?

  • NaN
  • 0
  • Infinity (correct)
  • Undefined
  • Which of the following is a common mathematical constant in JavaScript that represents the ratio of the circumference of a circle to its diameter?

  • golden ratio
  • e
  • tau
  • pi (correct)
  • Which of the following functions in JavaScript can be used to round a number to the nearest integer?

  • Math.ceil()
  • Math.random()
  • Math.floor()
  • Math.round() (correct)
  • Which mathematical function would you use in JavaScript to find the sine of a number?

    <p>Math.sin()</p> Signup and view all the answers

    What method would you use to generate a random number between 0 and 1 in JavaScript?

    <p>Math.random()</p> Signup and view all the answers

    Which of the following statements about accessing array values is correct?

    <p>The index value for the last element of an array is equal to the array's length.</p> Signup and view all the answers

    What would be the output of accessing the value at index 2 in the groceries array?

    <p>Frosted Flakes</p> Signup and view all the answers

    Which symbol is used to denote the start and end of an array in JavaScript?

    <p>[]</p> Signup and view all the answers

    If the groceries array is defined as var groceries = ['Milk', 'Eggs'], what will groceries[1] return?

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

    Which of these operations would yield an error when working with an array?

    <p>Accessing an out-of-range index.</p> Signup and view all the answers

    How do you create a non-empty array in JavaScript?

    <p>var groceries = ['item1', 'item2'];</p> Signup and view all the answers

    What is the index value of 'Salami' in the groceries array defined as var groceries = ['Milk', 'Eggs', 'Frosted Flakes', 'Salami', 'Juice'];?

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

    What does accessing an index beyond the length of an array return?

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

    Which method is used to add an item to the end of an array?

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

    What will the groceries array contain after executing groceries.push('Cookies'); with an initial length of 5?

    <p>6 items</p> Signup and view all the answers

    What happens to the index values of existing items when using unshift to add an item?

    <p>They all increase by 1</p> Signup and view all the answers

    What will the result of groceries.pop() if the groceries array has 6 items?

    <p>It will return the last item and remove it</p> Signup and view all the answers

    If groceries.unshift('Bananas') is called, what is the new value of groceries[0]?

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

    What does the length property of an array return?

    <p>The total number of index positions in the array, which may differ from the number of items</p> Signup and view all the answers

    Which method is appropriate to remove the first item from the array?

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

    How can you determine the total number of items in an array given its length property?

    <p>The length value directly indicates the number of items</p> Signup and view all the answers

    Which statement is true regarding the push and unshift methods?

    <p>Both return the new length of the array after the operation</p> Signup and view all the answers

    What will be the result of trying to access groceries[5] in an array defined as var groceries = ['Milk', 'Eggs', 'Frosted Flakes', 'Salami', 'Juice'];?

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

    Which of the following correctly represents the method used to add multiple items to the end of an array at once?

    <p>groceries.push('Butter', 'Bread')</p> Signup and view all the answers

    If you have an array defined as var groceries = ['Milk', 'Eggs']; and you execute groceries.unshift('Yogurt'), what will groceries[0] now return?

    <p>'Yogurt'</p> Signup and view all the answers

    Which method would you use to remove the last item from the groceries array?

    <p>groceries.pop()</p> Signup and view all the answers

    Given var groceries = ['Milk', 'Eggs', 'Frosted Flakes', 'Salami', 'Juice']; what will the result of groceries.length be?

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

    Which of these statements regarding the creation of an array is correct?

    <p>An empty array is defined as var groceries = []</p> Signup and view all the answers

    If groceries is defined as var groceries = ['Milk', 'Eggs', 'Salami'], what will the result of groceries.push('Juice'); followed by groceries[3] be?

    <p>'Juice'</p> Signup and view all the answers

    What will be the content of the variable goodAndBad after executing the statement var goodAndBad = good.concat(bad);?

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

    If you want to increment the variable stooges by 2 in JavaScript, which of the following statements is correct?

    <p>stooges = stooges + 2;</p> Signup and view all the answers

    In the operation var remainder = total % 7, what does the '%' operator compute?

    <p>The remainder when total is divided by 7</p> Signup and view all the answers

    Given the expression var more = (1 + average * 10) / 5, which order of operations is being applied?

    <p>Add, multiply, then divide</p> Signup and view all the answers

    Which of the following statements correctly defines the result of the statement i = i - 2?

    <p>It decreases i by 2 regardless of its initial value.</p> Signup and view all the answers

    What will be the index value of the last item after applying the pop method on an array with 7 items?

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

    What happens to the total number of items in the array after using the push method?

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

    If groceries is an array with 4 items, what will be the value of groceries.length if we apply unshift to add an item?

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

    Which method would correctly remove the first item from the groceries array?

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

    If you call groceries.pop() on an empty array, what will the result be?

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

    What will the state of the groceries array be after executing groceries.unshift('Apples'); assuming it had 3 items?

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

    When using the length property on an array, what does it actually count?

    <p>The number of index positions</p> Signup and view all the answers

    If you have an array defined as var groceries = ['Bread', 'Milk'], what will running groceries.unshift('Eggs') result in?

    <p>['Eggs', 'Bread', 'Milk']</p> Signup and view all the answers

    What value does the groceries array hold after executing groceries.push('Chips') on an initially empty array?

    <p>['Chips']</p> Signup and view all the answers

    What will be the output of groceries.pop() if the array has the elements ['Fish', 'Meat', 'Vegetables']?

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

    Study Notes

    Advanced Web Programming - Chapter 9: Arrays, Numbers

    • Arrays: Used to handle lists of data
    • Array Tasks: Can perform common tasks using various array properties
    • Numbers: Understand the variety of numerical values encountered
    • Math Object: Useful for mathematical operations

    Arrays

    • Concept: A list of items, similar to a numbered list on paper
    • Real-World Example: A grocery list
    • Elements: The items in the list
    • Array Values: The individual items in the array

    Creating Arrays

    • Empty Array: var groceries = [];
    • Literal Notation: Using brackets and commas to specify elements: var groceries = ["Milk", "Eggs", "Frosted Flakes"];

    Accessing Array Values

    • Index Values: Each item in an array has a numerical index (starting from 0).
    • Example: groceries[1] accesses the second array element
    • Using Index Values: Specify the index value within square brackets to access the element

    Accessing Array Values (Continued)

    • Looping: Use for loops to access all items programmatically
    • length Property: The array's length property provides the number of elements in the array. Iterating from 0 up to length - 1.

    Adding Items to an Array

    • push(): Adds elements to the end of the array: Groceries.push("Cookies");
    • unshift(): Adds elements to the beginning of the array: groceries.unshift("Bananas");
      • Both push() and unshift() return the new length of the array.

    Removing Items from an Array

    • pop(): Removes and returns the last element in the array
    • shift(): Removes and returns the first element in the array

    Removing Items from an Array (Continued)

    • slice(): A method to copy a portion of an array to a new array, without changing the original. 

    Finding Items in an Array

    • indexOf(): Returns the index of the first occurrence of a given element in the array, or -1 if not found
    • lastIndexOf(): Returns the index of the last occurrence of a given element in the array, or -1 if not found.

    Merging Arrays

    • concat(): Used to combine two or more arrays into a new array. 

    Numbers (Using a Number)

    • Declaration Examples:
      • var stooges = 3;
      • var pi = 3.14159;
      • var color = 0xFF;
      • var massOfEarth = 5.9742e+24;

    Operators - Doing Simple Math

    • Basic Operators: +, -, *, /, % (addition, subtraction, multiplication, division, modulus)
    • Order of Operations: Parentheses (), Exponents, Multiplication, Division, Addition, Subtraction (PEMDAS)

    Incrementing and Decrementing

    • Incrementing: Increasing a variable's value by a specific amount (often 1). i++ or i += 2
    • Decrementing: Decreasing a variable's value by a specific amount (often 1). i-- or i -= 2
    • Position of Operator: Affects whether the new value or the old value is returned.

    Special Values - Infinity and NaN

    • Infinity: Represents values that are very large. For example: Math.sqrt(-1).
    • NaN: "Not a Number" result from invalid numerical operations like 0/0 or Math.sqrt(-1)

    The Math Object

    • Functions and Constants: Useful tools for complex mathematical operations
    • Examples:
      • Math.floor() (rounds down to the nearest integer)
      • Math.ceil() (rounds up to the nearest integer)
      • Math.round() (rounds to the nearest integer, handling .5 cases)
      • Math.random() (generates a pseudo-random number)
      • Math.PI (constant value for pi)

    Trigonometric Functions

    • Radians: Trigonometric functions use radians
    • Examples:
      • Math.sin(), Math.cos(), Math.tan(), Math.acos(), Math.asin(), Math.atan()

    Powers and Square Roots

    • Using Math.pow(): Calculating exponents (e.g., 2 to the power of 4: Math.pow(2,4))
    • Using Math.exp(): Calculating Euler's constant (e) to an exponent.
    • Using Math.sqrt(): Calculating the square root of a number

    Getting the Absolute Value

    • Math.abs(): Returns the absolute value of a number (e.g., Math.abs(-5) returns 5).

    Random Numbers

    • Math.random(): Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive)

    Lab

    • Links: provide links to external resources for further learning and practice.

    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 Chapter 9 of Advanced Web Programming, focusing on arrays and numbers. You'll learn about the concept of arrays, how to create them, and how to access their values. Additionally, explore the Math object and its significance in mathematical operations.

    More Like This

    Arrays and Basic Addition Quiz
    5 questions
    1-D Arrays and Vectors in MATLAB Quiz
    10 questions
    Matrix Operations Quiz
    10 questions

    Matrix Operations Quiz

    EventfulPlatypus avatar
    EventfulPlatypus
    Advanced Web Programming - Chapter 9
    24 questions
    Use Quizgecko on...
    Browser
    Browser