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.
- Use the
String
split
method to turn your string variable from challenge #2 into an array.
- 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
```js 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
```js 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
```js friends.push("Me!"); friends.unshift("Someone else!"); ```- 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
```js var allFriends = myFriends.concat(yourFriends); allFriends.sort(); ```- This array contains 2 arrays, of different groups of friends. But everyone became friends! Merge these two arrays into a single array using array methods.
var friends = [
[
"George",
"John",
"Ringo",
"Paul"
], [
"Brian",
"Mick",
"Keith",
"Ian",
"Bill",
"Charlie"
]
]
answer
```js friends[0].concat(friends[1]); ```- We have an array of the cardinal directions. In one line, get the first letter of the word "north" from this array.
var directions = [
"north",
"south",
"east",
"west"
];
answer
```js directions[0][0]; ```- We need to use in-between directions, like "east northeast", when navigating. Build the string "east northeast" using this array.
answer
```js directions[2] + " " + directions[0] + directions[2]; ```- We can shorten east northeast into shorthand using just the first letters, ene. Build that string using this array.