forked from joinpursuit/8-0-reference-types-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (50 loc) · 1.83 KB
/
index.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
/**
* Adds a new store to the very end of the list.
* @param {Object[]]} stores - An array of store objects.
* @param {Object} store - An object representing a single store. See the instructions for details on its shape.
* @returns {Object[]} The same `stores` array that was inputted.
*/
function addNewStore(stores, store) {
stores.push(store)
return(stores)
}
/**
* Removes a store object at the given position.
* @param {Object[]]} stores - An array of store objects.
* @param {number} index - A number representing the index of the store to be removed from the array.
* @returns {Object[]} The same `stores` array that was inputted.
*/
function removeStoreAtPosition(stores, index) {
stores.splice(index , 1)//at index "index", remove 1 element and move to a new array.
return(stores)
}
/**
* Creates a duplicate of the `store` object. No references should be shared between the inputted `store` and the result.
* @param {Object} store - An object representing a single store. See the instructions for details on its shape.
* @returns {Object} The duplicated store object. This should not be the same as the store that was inputted.
*/
function duplicateStore(store) {
//create a new object
let newStore = {}
//set its values equal to the original stores values
newStore.name = store.name
//create an array for boardGames
newStore.boardGames = []
//go through the board games array and copy each element
for (let i = 0; i < store.boardGames.length; i++){
newStore.boardGames[i] = store.boardGames[i]
}
//make an object for address
newStore.address = {}
newStore.address.street = store.address.street
newStore.address.city = store.address.city
newStore.address.state = store.address.state
newStore.address.zip = store.address.zip
// return
return newStore
}
module.exports = {
addNewStore,
removeStoreAtPosition,
duplicateStore,
};