-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (149 loc) · 3.69 KB
/
app.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
/**
* Stores the list of kittens
* @type {Kitten[]}
*/
let kittens = [];
/**
* Called when submitting the new Kitten Form
* This method will pull data from the form
* use the provided function to give the data an id
* you can use robohash for images
* https://robohash.org/<INSERTCATNAMEHERE>?set=set4
* then add that data to the kittens list.
* Then reset the form
*/
function addKitten(event) {
event.preventDefault()
let form = event.target
let kitten = {
id: generateId(),
name: form.name.value,
mood: "tolerant",
affection: 0
}
kittens.push(kitten)
saveKittens()
form.reset()
}
/**
* Converts the kittens array to a JSON string then
* Saves the string to localstorage at the key kittens
*/
function saveKittens() {
window.localStorage.setItem("kitty", JSON.stringify(kittens))
drawKittens()
}
/**
* Attempts to retrieve the kittens string from localstorage
* then parses the JSON string into an array. Finally sets
* the kittens array to the retrieved array
*/
function loadKittens() {
let storedKittens = JSON.parse(window.localStorage.getItem("kitty"))
if (storedKittens) {
kittens = storedKittens
}
}
/**
* Draw all of the kittens to the kittens element
*/
function drawKittens() {
let kittenListElement = document.getElementById("kittens-list")
let kittensTemplate = ""
kittens.forEach(kitten => {
kittensTemplate += `
<div class "kitten-card">
<h3>${kitten.name}</h3>
<p>
<img src = "https://robohash.org/<${kitten.id}>?set=set4" alt="Your Kitty Pic">
</p>
<h4>Kitten Mood: '${kitten.mood}'</h4>
<h4>Affection Given: ${kitten.affection}</h4>
<button type="button" onclick="pet('${kitten.id}')">Pet Kitty</button>
<button type="button" onclick="catnip('${kitten.id}')">Give Catnip</button>
<button type="button" onclick="removeKitten('${kitten.id}')">Bye Bye Kitty</button>
</div>
`
})
kittenListElement.innerHTML = kittensTemplate
}
function removeKitten(kittenId) {
let index = kittens.findIndex(kitten => kitten.id == kittenId)
if (index == -1) {
throw new Error("Invalid Kitten Id")
}
kittens.splice(index, 1)
saveKittens()
}
/**
* Find the kitten in the array by its id
* @param {string} id
* @return {Kitten}
*/
function findKittenById(id) {
return kittens.find(k => k.id == id);
}
/**
* Find the kitten in the array of kittens
* Generate a random Number
* if the number is greater than .7
* increase the kittens affection
* otherwise decrease the affection
* save the kittens
* @param {string} id
*/
function pet(id) {
findKittenById()
console.log(id)
console.log(affection)
let randomNum = Math.random
if (randomNum > .7) {
kitten.affection++
}
else {
kitten.affection--
}
}
/**
* Find the kitten in the array of kittens
* Set the kitten's mood to tolerant
* Set the kitten's affection to 5
* save the kittens
* @param {string} id
*/
function catnip(id) {
console.log("You gave your kitty catnip!")
}
/**
* Sets the kittens mood based on its affection
* Happy > 6, Tolerant <= 5, Angry <= 3, Gone <= 0
* @param {Kitten} kitten
*/
function setKittenMood(kitten) {
}
/**function getStarted() {
document.getElementById("welcome").remove();
drawKittens();
}
/**
* Defines the Properties of a Kitten
* @typedef {{id: string, name: string, mood: string, affection: number}} Kitten
*/
/**
* Used to generate a random string id for mocked
* database generated Id
* @returns {string}
*/
function generateId() {
return (
Math.floor(Math.random() * 10000000) +
"-" +
Math.floor(Math.random() * 10000000)
);
}
/**function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
*/
loadKittens()
drawKittens()