Use the JavaScript console in your browser to solve the challenges. Press command + option + J
to open the console in Chrome. Feel free to also record your responses in a file, but make sure you test them in the console!
- Store your first name in a variable.
- Concatenate your first name with your last name, and store the result in another variable called
fullName
. - Use the
String
split method to turn yourfullName
variable from challenge #2 into an array with 2 elements – your first name and last name (e.g. 'Bob Smith' -->['Bob', 'Smith']
).
-
An array called
foods
holds the names of my top 20 favorite foods, starting with the best food. How can you find my fifth favorite food?answer
foods[4] -
Starting from the existing
friends
variable below, change the element that is currently "Elizabeth" to "Liz".var friends = [ "Moe", "Jane", "Emma", "Elizabeth", "Abanov", "Lycia" ];
answer
friends[3] = "Liz"; -
Using array methods, add your name to the end of the
friends
array, and add another name to beginning.hint
Look up array methods `push` and `unshift`.answer
friends.push("Me!"); friends.unshift("Someone else!"); -
Stretch: We have two lists of friends below. Use array methods to combine them into one alphabetically-sorted list.
var myFriends = [ "Rickon", "Meera", "Hodor", "Jojen", "Osha", "Rickard", "Maester" ]; var yourFriends = [ "Bilbo", "Boromir", "Elrond", "Faramir", "Frodo", "Gandalf", "Legolas", "Pippin" ];
hint
Look up array methods `concat` and `sort`.answer
var allFriends = myFriends.concat(yourFriends);
allFriends.sort();
-
Write out an object literal to represent the data below.
John, Doe, 36, 1234 Park St.
sample answer
var jd = { firstName: "John", lastName: "Doe", age: 36, address: { street: "Park St.", number: 1234 } }
-
How would you represent the following data using a combination of object literals and arrays? (You can describe a strategy without typing or writing out the whole thing.)
Jane, Doe, 32, 1239 Spark St. Mary, Doe, 31, 1231 Spark St. Greg, Doe, 34, 1214 Park St. Harriet, Doe, 32, 1324 Park St.
answer
Structure each object like the one I made for the last question. Then put all of the objects inside one array.
Copy the following clubs
variable into your console.
var clubs = [
{
name: 'Yearbook',
students: [
{ first: 'Joe', last: 'Lakin' },
{ first: 'Evalyn', last: 'Bradtke' },
{ first: 'Samuel', last: 'Black' }
],
teacher: 'James Friar'
},
{
name: 'Jazz Band',
students: [
{ first: 'Douglas', last: 'Wisoky' },
{ first: 'Cora', last: 'Thompson' },
{ first: 'Samuel', last: 'Ziemann' },
{ first: 'Alana', last: 'Cortez'}
],
teacher: 'Luther Richards'
},
{
name: 'Swim Team',
students: [
{ first: 'Cora', last: 'Thompson' },
{ first: 'Samuel', last: 'Black' },
{ first: 'Alana', last: 'Cortez'},
{ first: 'Joe', last: 'Lakin' }
],
teacher: 'Carol Darby'
}
];
Start from the clubs
variable.
-
Find and
console.log
the following:-
the array that contains all the student club data
answer
console.log(clubs); -
the number of clubs
answer
console.log(clubs.length); -
the object that contains all of the information for the jazz band
answer
console.log(clubs[1]); -
the teacher of the first club
answer
console.log(clubs[0]['teacher']); // bracket notation, or console.log(clubs[0].teacher); // dot notation
-
the array of students in the jazz band
answer
console.log(clubs[1]['students']); console.log(clubs[1].students);
-
the last name of the second student on the swim team
answer
console.log(clubs[2]['students'][1]['last']); console.log(clubs[2].students[1].last);
-
-
Create an object literal representing a student with your name, and assign it to a variable.
answer
var me = { first: 'Bob', last: 'Loblaw' }; -
Add yourself to one of the clubs as a student member.
answer
// joining the swim team clubs[2]['students'].push(me); // or clubs[2].students.push(me);
-
Create an object literal representing a new club, and assign it to a variable. Make sure it has values for name, students, and teacher.
answer
var lawClub = { name: 'Legal Eagles', students: [], teacher: 'Abby Fuentes' };
-
Use an array method to add your new club to the array of clubs.
answer
clubs.push(lawClub);
-
Add yourself as a student in the new club.
answer
clubs[3]['students'].push(me); // or clubs[3].students.push(me);
-
-
Update Samuel Black's first name to Sam everywhere it occurs. Hint: there is not a great shortcut to do this.
answer
clubs[0]['students'][2]['first'] = 'Sam'; clubs[2]['students'][1]['first'] = 'Sam'; clubs[0].students[2].first = 'Sam'; clubs[2].students[1].first = 'Sam';
-
Oops, the school is losing extracurricular funding. Use an array method to remove one of the clubs from the array.
answer
`clubs.shift(); // goodbye yearbook!`