-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
174 lines (125 loc) · 4.96 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Array for Days!!
// For any methods that we didn't cover, please refer to the Array Documentation.
// Discovering new things is fun and a great way to learn!!
// Please console.log("Question#") followed by console.log(yourAnswer) after each question.
// If you would like, practice using document.write() as well. Use any elements you like!
// ==========================================================================
// 1. Declare a variable whose value is an empty Array.
// Use any method you choose to add at least 4 items to it.
console.log("Question #1");
var breakfastFood = [];
breakfastFood.push('pancakes', 'eggs', 'sausage', 'orange juice');
console.log(breakfastFood);
// 2. Add an additional item to the beginning of your Array.
console.log("Question #2");
breakfastFood.unshift('milk');
console.log(breakfastFood);
// 3. Remove the second and third items.
console.log("Question #3");
breakfastFood.splice(1, 2);
console.log(breakfastFood);
// 4. Add two new items after the second item.
console.log("Question #4");
breakfastFood.splice(2, 0, 'bacon', 'oatmeal');
console.log(breakfastFood);
// 5. Write 'The current length of the array is....' using the .length method
console.log("Question #5");
console.log('The current length of the array is ' + breakfastFood.length);
// Use the following Array for questions 6-11:
var things = ['mug', 'book', 'mouse', 'plant', 'sunglasses'];
// 6. Change 'mouse' to 'keyboard'
console.log("Question #6");
things[2] = 'keyboard'; //redefining the item, replacing the value
console.log(things);
// 7. Combine all of the elements of the array into a string.
console.log("Question #7");
console.log(things.join());
// 8. Declare a variable called lastItem whose using .pop()
// Add two new items to lastItem, one at the beginning and one at the end.
console.log("Question #8");
lastItem = [things.pop()]; // house the last item of things in brackets
lastItem.push('cat');
lastItem.unshift('charger');
console.log(lastItem);
// 9. Create a new Array called itemLast.
// The items should be the same as lastItem, only in reverse order.
console.log("Question #9");
itemLast = lastItem.reverse();
console.log(itemLast);
// 10. Remove the first item of itemLast.
console.log("Question #10");
removeFirst = itemLast.shift();
console.log(removeFirst);
console.log(itemLast);
// 11. Remove all items from itemLast
// (No need to write to the document. Just console.log to test your results)
console.log("Question #11");
itemLast = [];
console.log(itemLast);
// 12. Using the Arrays below, create a single Array
// called numberPets whose value is [12, 5, 9, 27, 'fish', 'dog']
console.log("Question #12");
var firstArray = [12, 5, 9, 27];
var secondArray = ['fish', 'dog'];
var numberPets = firstArray.concat(secondArray);
console.log(numberPets);
// Use the following array for questions 13-16:
var people = ['Bill', 'Ted', 'Emily', 'Andrea', 'Doug'];
// 13. Add two new people after 'Doug'
console.log("Question #13");
people.splice(5, 0, 'Andy', 'Mark');
console.log(people);
// 14. Remove everybody except 'Andrea' and 'Ted'
console.log("Question #14");
// var newPeople = [];
// for (var i=0; i < people.length; i++){
// if( (people[i] == 'Andrea') || (people[i] == 'Ted') ){
// newPeople += people[i];
// newPeople += ' ';
// }
// }
// var people = newPeople.split(" ");
// people.pop();
people = [people[1], people[3]];
console.log(people);
// 15. Add a new person to the beginning of the Array
console.log("Question #15");
people.unshift('Michelle');
console.log(people);
// 16. Arrange the items alphabetically. Store this Array as orderedPeople
console.log("Question #16");
orderedPeople = people.sort();
console.log(orderedPeople);
// 17. Create an array of arrays with the following three arrays:
console.log("Question #17");
var array1 = ["Fido", "Spot", "Rex", "Sparky"]
var array2 = ["Bulldog", "Lab", "Dalmation", "Beagle"]
var array3 = ["White", "Black", "Spotted", "Tri-color"]
// var array4 = [];
// array4.push(array1, array2, array3);
var array4 = [array1, array2, array3];
console.log(array4);
// Goal:
var array4 = [
["Fido", "Spot", "Rex", "Sparky"],
["Bulldog", "Lab", "Dalmation", "Beagle"],
["White", "Black", "Spotted", "Tri-color"]
]
// 18. Remove "Sparky" and "White" from the above array of arrays.
console.log("Question #18");
array4[0].pop();
array4[2].shift();
console.log(array4);
// BONUS 1: Try to arrange the following items from smallest to largest:
console.log("Question #19 - Bonus");
var sortingNumbers = [2, 5, 98, 55, 77, 300]
// Explain why it doesn't sort as expected.
sortingNumbers.sort(function(a, b) {
return a - b
});
console.log (sortingNumbers);
// BONUS 2: Transform array1 into array2 using as few lines of code as you can without directly changing the value of an item (ie array1[0] = item)
console.log("Question #20 - Bonus");
var array1 = [2, 'dog', 34, 'Bill', 'plant', 'mug', 17];
// Goal:
var array2 = ['plant', 17, 2, 'Bill', 'dog'];