This worksheet will double as Javascript notes for future reference! Copy it into your preferred note-taking program at the end of class.
For each expression, predict what you think the output will be in a comment (//) without first running the command. Then run the expression in the console. Note the actual output in a comment and compare it with your prediction.
typeof("potato")
// Prediction: Vegetable
// Actual: StringWhat is the output of each of the expressions below?
typeof(15)
// Prediction:
// Actual:
typeof(5.5)
// Prediction:
// Actual:
typeof(NaN)
// Prediction:
// Actual:
typeof("hello")
// Prediction:
// Actual:
typeof(true)
// Prediction:
// Actual:
typeof(1 != 2)
// Prediction:
// Actual:
"hamburger" + "s"
// Prediction:
// Actual:
"hamburgers" - "s"
// Prediction:
// Actual:
"1" + "3"
// Prediction:
// Actual:
"1" - "3"
// Prediction:
// Actual:
"johnny" + 5
// Prediction:
// Actual:
"johnny" - 5
// Prediction:
// Actual:
99 * "luftbaloons"
// Prediction:
// Actual:What's going on in the second half of the previous question? Are there any "rules" we can pull from those examples?
Data structures include arrays and objects. We will go over objects in a later class.
Javascript provides us with a number of native methods that allow us to interact with arrays. Find methods that do each of the following and provide an example...
- Add an element to the back of an array.
- Remove an element from the back of an array.
- Add an element to the front of an array.
- Remove an element from the front of an array.
- Concatenates all the elements in an array into a string.
- Separates the characters of a string into an array. This one is a string method.
This is a great exercise for practicing your "Google Fu"! If you need a starting point, check out MDN's documentation page on arrays.
// Your answers go here.What will the contents of the below arrays be after the code samples are executed? Come up with an answer yourself before testing it out in the console.
var numbers = [2, 4, 6, 8]
numbers.pop()
numbers.push(10)
numbers.unshift(3)Your answer goes here.
What is the return value of the below code sample? Come up with an answer yourself before testing it out in the console.
var morse = ["dot", "pause", "dot"]
var moreMorse = morse.join(" dash ")
moreMorse.split(" ")Your answer goes here.
What will the contents of the below array be after the below code sample is executed? Come up with an answer yourself before testing it out in the console.
var bands = []
var beatles = ["Paul", "John", "George", "Pete"]
var stones = ["Brian", "Mick", "Keith", "Ronnie", "Charlie"]
bands.push(beatles)
bands.unshift(stones)
bands[bands.length - 1].pop()
bands[0].shift()
bands[1][3] = "Ringo"Your answer goes here.
Here's an example truth table for the ! (not) operation. In it, we're listing the values of !a that correspond with a given value of a.
| a | !a |
|---|---|
| true | false |
| false | true |
Fill out the truth tables below for && (and), || (or) and one that uses multiple comparison operators. All you need to do is replace the ?'s with either true or false.
NOTE: Because of markdown formatting,
||and&&have been replaced withORandANDrespectively.HINT: With the last one, it may be helpful to add additional columns to the table for each individual comparison.
| a | b | a AND b |
|---|---|---|
| true | true | ? |
| true | false | ? |
| false | true | ? |
| false | false | ? |
| a | b | a OR b |
|---|---|---|
| true | true | ? |
| true | false | ? |
| false | true | ? |
| false | false | ? |
| a | b | a != b |
|---|---|---|
| 3 | 3 | ? |
| 1 | 5 | ? |
| 2 | "2" | ? |
| a | b | !a AND (a OR b) |
|---|---|---|
| true | true | ? |
| true | false | ? |
| false | true | ? |
| false | false | ? |
You're a bouncer at a bar. Given an age variable, create a conditional that satisfies the following requirements...
- If a patron is older than
21, print out"Come on in!". - If a patron is between
18and21, print out"Come on in (but no drinking)!". - If a patron is younger than 18, print out
"You're too young to be in here!". - If a patron is older than 75, print out
"Are you sure you want to be here?".
// Your answer goes here.Bar patrons must have an ID if the bouncer is even going to consider what age they are.
- If the patron has an ID, the bouncer will then check if they are of the proper age
- If the patron does not have an ID, the bouncer will tell them
"No ID, no entry."
Hint: Whether the patron has an ID or not can be stored in a
hasIdvariable. What do you think the stored data type should be?