-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
217 lines (193 loc) · 5.73 KB
/
kata.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Objects, keys/properties and values
*/
// getGreeting should return a string containing 'Hello ' and the contents of `name`
function getGreeting(name) {
return "Hello " + name;
}
// ageOneYear should add one to the `age` property of `obj`
function ageOneYear(obj) {
return (obj.age += 1);
}
// makeObject should return an object that looks like this:
// (but using the arguments passed to the function)
// {
// key: value
// }
function makeObject(key, value) {
var obj = {};
obj[key] = value;
return obj;
}
// getValue should return the value of the property contained in `key`
function getValue(obj, key) {
console.log(obj[key]);
return obj[key];
}
// addName should add a `name` property to the object with the value of the `name` argument
function addName(obj, name) {
console.log(name);
return (obj.name = name);
}
// deleteProperty should remove the property contained in `key` from `obj`
// Tip: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
function deleteProperty(obj, key) {
console.log(obj[key]);
return delete obj[key];
}
// returnErrorIfFalsy should return a JavaScript Error object with message:
// 'Oh no, an error!'
// if val evaluates to false
// Tip: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
function returnErrorIfFalsy(val) {
if (!val) {
return Error("Oh no, an error!");
}
}
// keys should return an array of the object's property names (keys)
// For example, given {foo: 1, bar: 2} it would return ['foo', 'bar']
function keys(obj) {
console.log(Object.keys(obj));
return Object.keys(obj);
}
// values should return an array of the object's own values
// For example, given {foo: 1, bar: 2} it would return [1, 2]
function values(obj) {
console.log(Object.values(obj));
return Object.values(obj);
}
/**
* Arrays
*/
// makeArrayOfItem should return an array that is `length` long, made up of `item`
// For example, makeArrayOfItem('foo', 2) would return:
// ['foo', 'foo']
function makeArrayOfItem(item, length) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(item);
}
return arr;
}
// makeArrayOfItems should return an array containing all arguments passed to it
// Tip: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
function makeArrayOfItems() {
const arr = [];
for (let value in arguments) {
arr.push(arguments[value]);
}
return arr;
}
// hasItem should return true if `item` is present in `arr` at least once,
// otherwise it should return false.
function hasItem(arr, item) {
return arr.includes(item);
}
// getItemAtIndex should return arr[idx] but only if that index exists:
// if it doesn't, return a JavaScript Error object.
function getItemAtIndex(arr, idx) {
console.log(arr[idx]);
if (arr[idx]) {
return arr[idx];
} else {
return Error();
}
}
// replaceItemAtIndex should replace the element at `idx` with `item`
function replaceItemAtIndex(arr, idx, item) {
return (arr[idx] = item);
}
// insertItemAtIndex should insert `item` at `idx` without overwriting
// any array values (the array should get longer)
function insertItemAtIndex(arr, item, idx) {
arr.splice(idx, 0, item);
// at index postion idx(3), remove 0 elements, add item
console.log(arr);
return arr;
}
// deleteItemAtIndex should remove the element at `idx` (the array
// should get shorter).
function deleteItemAtIndex(arr, idx) {
arr.splice(idx, 1);
// at index position remove 1 element
return arr;
}
// deleteItem should return an array with every instance of `item` removed
function deleteItem(arr, item) {
return arr.filter(x => {
return x !== item;
});
}
// zipObject should return an object built from two arrays
// For example, given ['foo', 'bar'] and [1, 2] it would return
// {foo: 1, bar: 2}
function zipObject(keys, values) {
var result = {};
keys.forEach((key, i) => (result[key] = values[i]));
console.log(result);
return result;
}
// unzipObject should return an array of arrays, each one a pair of keys and values
// For example, given {foo: 1, bar: 2} it would return
// [['foo', 1], ['bar', 2]]
function unzipObject(obj) {
var arr = [];
for (var x in obj) {
var child = [x, obj[x]];
arr.push(child);
console.log(child);
}
return arr;
}
// findOneByProperty should return an object from `arr` that has the
// property AND value of `search`. For example, given:
// [{a: 1}, {b: 2, c: 3}] and {b: 2}
// it will return:
// {b: 2, c: 3}
function findOneByProperty(arr, search) {
var found = null;
var prop = Object.keys(search)[0];
arr.forEach(function(item) {
var itemProp = Object.keys(item)[0];
if (itemProp === prop && item[prop] === search[prop]) {
found = item;
}
});
return found;
}
// findAll should return an array containing all objects in `arr` that
// have the property and value of `search`
function findAll(arr, search) {
const found = [];
const prop = Object.keys(search)[0];
arr.forEach(function(item) {
const hasKey = Object.keys(item).includes(prop);
if (hasKey && item[prop] === search[prop]) {
found.push(item);
}
});
return found;
}
module.exports = {
addName: addName,
ageOneYear: ageOneYear,
deleteItem: deleteItem,
deleteItemAtIndex: deleteItemAtIndex,
deleteProperty: deleteProperty,
getGreeting: getGreeting,
getItemAtIndex: getItemAtIndex,
getValue: getValue,
findOneByProperty: findOneByProperty,
findAll: findAll,
hasItem: hasItem,
insertItemAtIndex: insertItemAtIndex,
keys: keys,
makeArrayOfItem: makeArrayOfItem,
makeArrayOfItems: makeArrayOfItems,
makeObject: makeObject,
replaceItemAtIndex: replaceItemAtIndex,
returnErrorIfFalsy: returnErrorIfFalsy,
unzipObject: unzipObject,
values: values,
zipObject: zipObject
};