forked from alex-aaron/matchy-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
127 lines (112 loc) · 3.87 KB
/
data.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
/**
* Part 1
*
* In this file, we're going to practice
* creating and accessing data structues.
*
* See the README for detailed instructions,
* and read every instruction carefully.
*/
//////////////////////////////////////////////////////////////////////
// Step 1 - Object Creation //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
var animal = {};
animal.species = 'Penguin';
animal['name'] = 'Puffin';
animal.noises = [];
console.log(animal);
//////////////////////////////////////////////////////////////////////
// Step 2 - Array Creation ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
var noises = [];
noises[0] = 'braying';
noises.push('honk');
noises.unshift('chirp');
noises[noises.length] = 'trumpet';
console.log(noises.length);
console.log(noises.length - 1);
console.log(noises);
//////////////////////////////////////////////////////////////////////
// Step 3 - Combining Step 1 and 2 ///////////////////////////////////
//////////////////////////////////////////////////////////////////////
animal['noises'] = noises;
console.log(animal);
animal['noises'].push('bark');
console.log(animal);
/* *******************************************************************
* Step 4 - Review
*
* 1. What are the different ways you can access properties on objects?
*
* 2. What are the different ways of accessing elements on arrays?
*
* *******************************************************************
*/
/* *******************************************************************
* Step 5 - Take a Break!
*
* It's super important to give your brain and yourself
* a rest when you can! Grab a drink and have a think!
* For like 10 minutes, then, BACK TO WORK! :)
* *******************************************************************
*/
//////////////////////////////////////////////////////////////////////
// Step 6 - A Collection of Animals //////////////////////////////////
//////////////////////////////////////////////////////////////////////
var animals = [];
animals.push(animal);
console.log(animals);
var duck = {
species: 'duck',
name: 'Jerome',
noises: ['quack', 'honk', 'sneeze', 'woosh']
};
animals.push(duck);
console.log(animals);
var dog = {
species: 'dog',
name: 'Butters',
noises: ['bark', 'snuff']
};
var cat = {
species: 'cat',
name: 'Mr. Buggs',
noises: ['purr', 'meow']
};
animals.push(dog);
animals.push(cat);
console.log(animals);
console.log(animals.length);
//////////////////////////////////////////////////////////////////////
// Step 7 - Making Friends ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//the data structure I choose is array.
//I choose array because the date within can always be mutated if need be.
var friends = [];
function getRandom(array) {
return Math.floor(Math.random() * array.length);
}
console.log(getRandom(animals));
var randomAnimal;
randomAnimal = animals[getRandom(animals)];
console.log(randomAnimal);
friends.push(randomAnimal.name);
console.log(friends);
animals[getRandom(animals)].friends = friends;
console.log(animals);
/**
* Nice work! You're done Part 1. Pat yourself on the back and
* move onto Part 2 in the file called "functions.js"
*/
//////////////////////////////////////////////////////////////////////
// DON'T REMOVE THIS CODE ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
var animal, noises, animals, friends, getRandom;
if((typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')) {
module.exports.animal = animal || null;
module.exports.noises = noises || null;
module.exports.animals = animals || null;
module.exports.friends = friends || null;
module.exports.getRandom = getRandom || null;
}