forked from Montana-Code-School/JSBasics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.js
More file actions
179 lines (137 loc) · 4.78 KB
/
basics.js
File metadata and controls
179 lines (137 loc) · 4.78 KB
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
175
176
177
178
179
// This is a general practice file for JavaScript Basics. Please follow the
// instructions thoughout the sheet. Do not uncomment the commented out spaces.
var arr, findTwo, pusher, upFront, remover, copyCat, chopIt, sumIt, evenOdd,
timesTen, animal, findAge, getKeys, changeName, speechMaker, addKey, keyLoop,
cars, findModels;
// Arrays
// 1. Create the variable arr and assign it an array with the following list of
// numbers 12, 42, 36, 51, 96
var arr = [12, 42, 36, 51, 96];
// 2. Create a function called findTwo that takes in the arr array and returns
// the value at index 2.
function findTwo(array){
return array[2];
};
findTwo(arr);
// 3. Create a function called pusher that takes in an array and a number and
// positions the number at the end of the array. Return the new array.
function pusher(arr, num) {
arr.push(num);
return arr;
}
// 4. Create a function called upFront that takes in an array and a number and
// positions the number in beginning of the array. Return the new array.
function upFront(arr, num) {
arr.unshift(num);
return arr;
}
// 5. Create a function called remover that takes in an array and removes the
// value at the end of the array. Return the new array.
function remover(arr, num) {
arr.pop(num);
return arr;
}
// 6. Create a function called copyCat that takes in an array, copies, and
// returns the second and third index values.
function copyCat (arr) {
newArray = arr.slice(2,4);
return newArray;
}
// 7. Create a function called chopIt that takes in an array and two numbers.
// It deletes the original 3rd index value and inserts the two numbers in it's place.
function chopIt(arr, num1, num2) {
arr.splice(3, 1, num1, num2);
return arr;
}
// 8. Create a function called sumIt that takes in the array and returns the sum
// of each of the values in the array.
function sumIt(arr) {
var sum = arr.reduce(function(total, num) {
return total + num;
}, 0);
return sum;
}
// 9. Create a function called evenOdd that takes in the array and pushes into a
// new array "even" or "odd" for every value in the array, depending upon
// whether they are even or odd number values. Return the new array.
// Hint: Use the modulus operator
//#9 NOT WORKING
// function evenOdd(arr) {
// var oddEvenArray = arr.map(function(num) {if (num % 2 == 0) {arr.push("even");
// } else {
// arr.push("odd")};
// }
// return oddEvenArray;
// }
//
// 10. Create a function called timesTen that takes in an array and multiplies
// each value by 10 and returns the new array.
// Hint: Use the map method
function timesTen(arr) {
var timesArray = arr.map(function(num) {
return num * 10;
});
return timesArray;
}
// Objects
// 1. Create an object called animal. Make sure the animal has the following
// keys. (species, name, age, gender, favoriteFood) Also, give the animal a
// method of speech that returns a string that displays what the animal says.
var animal = {
species: "Canis lupus familiaris",
name: "Fido",
age: 2,
gender: "female",
favoriteFood: "bone",
speech: function () {
return "bow wow";
}
}
// 2. Create a function called findAge that takes in an object and returns the
// age key value.
function findAge(obj) {
return obj.age;
}
// 3. Create a function called getKeys that takes in an object and returns all
// of the key names (not values!).
function getKeys(obj) {
return Object.keys(obj);
}
// 4. Create a function called changeName that takes in an object and a name
// value and replaces the object name with the given name value. Return the new object.
function changeName(obj, name1) {
newObj = Object.defineProperty(obj, name);
}
// 5. Create a function called speechMaker that takes in an object and returns
// the response from calling the speech method.
// 6. Create a function called addKey that takes in an object and adds a new key
// name bedtime and assigns it a value. Return the object.
// 7. Create a function called keyLoop that takes in an object and loops through
// each of the key values. If any of the key values are equal to "monkey", then
// return "There's a monkey!", otherwise return "There's no monkey here!".
// Intermediate Arrays and Objects
// 1. Create an array called cars of 3 objects of 3 different cars with the key
// names of make, model, year.
// 2. Create a function called findModels that takes in an array, loops through
// the array and returns an array of all of the model values of each object.
module.exports = {
arr: arr,
findTwo: findTwo,
pusher: pusher,
upFront: upFront,
remover: remover,
copyCat: copyCat,
chopIt: chopIt,
sumIt: sumIt,
evenOdd: evenOdd,
timesTen: timesTen,
animal: animal,
findAge: findAge,
getKeys: getKeys,
changeName: changeName,
speechMaker: speechMaker,
addKey: addKey,
keyLoop: keyLoop,
cars: cars,
findModels: findModels
}