Bracket Notation!
Goals:
- Use bracket notation to access and overwrite elements of an array
Coding Snippets:
- An array is an ordered list of values. Each value, or element, has a corresponding index that refers to its position in the list.
- Bracket notation allows us to reference specific elements of an array using indexes, instead of resorting to array destructuring.
- Arrays are 0-indexed, meaning the first element has index 0. Thus,
dimensions[0]
references the page width.
Math and Computer Concepts:
- Bracket notation is the conceptual equivalent of the subscripts used in math to denote elements in a list. For example,
x[0]
andx[1]
correspond to x0 and x1. Moreover, these expressions are pronounced the same way, i.e., "x sub zero" and "x sub one". - Bracket notation makes it possible to iterate over two or more arrays simultaneously. For example, given corresponding arrays
grade
andweight
, we can compute a weighted average as follows:
Activities:
Save each of the following in a folder called BracketNotation.
BirthdayDay: Prompt the user to enter their birthday (e.g., as a string in MM/DD/YYYY format) then report back the day of week in which the person was born. The key to computing the day of week is to make use of a JavaScript
Date
object'sgetDay
method, which reports numerical values for the day of week (0 through 6). You will need to translate this numeric value to the corresponding string. Hint: define an array of weekday names and use subscripts!- SubscriptBoxes: Jump the turtle to a random position,and record its position in an array, i.e.,
loc=getxy()
. Draw a rectangle formed by reflecting this point across the horizontal and vertical center lines in the page. Do not use array destructuring. Instead, use subscripts to access the individual coordinates in the array. - TurtleRace: write an updated variant of this PencilCoder classic. Create all racing turtles anonymously, adding them to an array using the Array
push
method. All subsequent references to these turtles should be made using subscripts. - Write a program that contains functions to encrypt and decrypt strings using a Caesar Cipher. (If you are not familiar with this encryption technique, consult the relevant entry on wikipedia.) Each function should accept two arguments: a string and a number, with the latter representing the offset used in the encryption. As an added challenge, code a working Caesar cipher wheel.