Escape Sequences in Strings
Escape Sequences in Strings
Escape Sequences in Strings
Data types:
Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with
a number.
.shift() works just like .pop(), except it removes the first element instead of the last.
Stand in Line
Write a function nextInLine which takes an array (arr) and a number (item) as arguments.
Add the number to the end of the array, then remove the first element of the array.
The nextInLine function should then return the element that was removed.
arr.push(item);
return arr.shift();
return item;
Examples
1 != 2 // true
1 != "1" // false
1 != '1' // false
1 != true // false
0 != false // false
Examples
3 !== 3 // false
3 !== '3' // true
4 !== 3 // true
The next code will only return "Yes" if num is greater than 5 and less than 10:
The function is executed from top to bottom so you will want to be careful of what statement comes
first.
function foo(x) {
if (x < 1) {
return "Less than one";
} else if (x < 2) {
return "Less than two";
} else {
return "Greater than or equal to two";
}
}
caseInSwitch(1);
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
// Setup
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
Record Collection
You start with an updateRecords function that takes an object like collection, an id, a prop (like artist or
tracks), and a value. Complete the function using the rules below to modify the object passed to the
function.
• • Your function must always return the entire object.
• • If prop isn't tracks and value isn't an empty string, update or set that album's prop to
value.
• • If prop is tracks but the album doesn't have a tracks property, create an empty array and
add value to it.
• • If prop is tracks and value isn't an empty string, add value to the end of the album's
existing tracks array.
• • If value is an empty string, delete the given prop property from the album.
// Setup
var collection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
return object;
}
Remember that arrays have zero-based indexing, which means the last index of the array is length - 1.
Our condition for this loop is i < arr.length, which stops the loop when i is equal to length. In this case
the last iteration is i === 4 i.e. when i becomes equal to arr.length and outputs 6 to the console.
Declare and initialize a variable total to 0. Use a for loop to add the value of each element of the myArr
array to total.
// Setup
var myArr = [ 2, 3, 4, 5, 6];
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
product = product * arr[i][j];
}
}
// Only change code above this line
return product;
}
multiplyAll([[1,2],[3,4],[5,6,7]]);
function randomFraction() {
return Math.random();
function randomWholeNum() {
Create a function called randomRange that takes a range myMin and myMax and returns a random
whole number that's greater than or equal to myMin, and is less than or equal to myMax, inclusive.
unction convertToInteger(str) {
return parseInt(str, 2)
}
convertToInteger("10011");
Use the Conditional (Ternary) Operator
The conditional operator, also called the ternary operator, can be used as a one line if-else expression.
The syntax is:
condition ? statement-if-true : statement-if-false;
The following function uses an if-else statement to check a condition:
function findGreater(a, b) {
if(a > b) {
return "a is greater";
}
else {
return "b is greater";
}
}