Repository navigation
Expand file tree
/
Copy patharraysandobjects.js
More file actions
146 lines (137 loc) · 4.12 KB
/
Copy patharraysandobjects.js
File metadata and controls
146 lines (137 loc) · 4.12 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
let party = [
{
name: 'Joline',
hitpoints: 15,
belongings: ["spear", "bread", "Tums"],
companion: {
name: "Tabby",
type: "cat",
}
},
{
name: 'Timothy',
hitpoints: 10,
belongings: ["sword", "potion", "Tums"],
companion: {
name: "Velma",
type: "Bat",
}
},
{
name: 'Sarah',
hitpoints: 20,
belongings: ["bow", "arrows", "wood"],
companion: {
name: "Tony",
type: "tiger",
}
},
]
//Prompt 1: double the hitpoints of everyone in the party
party.forEach(el => {
el.hitpoints = 2*el.hitpoints
});
console.log(party);
// Prompt 2: Timothy has been hit with an arrow, subtract 5 points from his hp
const takeDamage = (name, hp) => {
party.forEach(element => {
if(element.name == name){
element.hitpoints = element.hitpoints - hp;
}
});
}
takeDamage('Timothy', 10);
console.log(party[1].hitpoints);
// Prompt 3: Sarah's tiger has been turned into a jellyfish by a wizard, please change it
party[2].companion.type = "jellyfish";
console.log(party[2]);
// Prompt 4: Timothy drank this potion. Raise his hitpoints by 20 and remove "potion" from his belongings.
const alteringhpandbelongings = (name, hp) =>{
party.forEach(element => {
if(element.name == name){
element.hitpoints = element.hitpoints + hp;
element.belongings.splice(1, 1);
}
});
}
alteringhpandbelongings('Timothy', 20);
console.log(party[1]);
// Prompt 5: Timothy got hungry and stole Joline's bread. Take it out of her belongings and put it into Timothy's belongings.
const assignedbelongings = (name, personstolefrom) => {
party.forEach(element => {
if (element.name == personstolefrom) {
stolenitem = element.belongings.splice(1, 1);
}
else if (element.name == name) {
element.belongings.push(...stolenitem);
}
});
}
assignedbelongings("Timothy", "Joline");
console.log(party);
// Prompt 6: Joline got upset and left the party. Take her out of the array. (use the name of the leaving member as a parameter)
const leavingmember = (name) => {
party.forEach(element => {
if (element.name == name) {
party.splice(0, 1);
}
});
}
leavingmember('Joline');
console.log(party);
// Prompt 7: Timothy and Sarah have been recruiting. Add a new adventurer to the party. (new adventurer is parameter)
var data = {};
data = {
name: 'Melanie',
hitpoints: 10,
belongings: ["bat", "magic", "medicine"],
companion: {
name: "noodle",
type: "Bird",
}
}
addAdventurer = (newadventurer) => party.push(newadventurer);
addAdventurer(data)
console.log(party);
// Prompt 8: The party has been doing well! They found 200 gold. Create a new property called gold and split the gold evenly between everyone. (amount of gold is parameter)
total = 200/3
const addGold = (amountofgold) => {
party.forEach(element => {
if (element.gold == undefined) {
element.gold = amountofgold
}
});
}
addGold(total)
console.log(party)
// Prompt 9: Sarah is tired of taking care of a jellyfish. Subtract some gold from her and change her companion to a bear.
party.forEach(element => {
if(element.name == "Sarah"){
element.gold = element.gold - 10
element.companion.type = "bear"
}
});
console.log(party);
// Prompt 10: Timothy’s sword has gotten old. Change it to “Rusty Sword"
party.forEach(element => {
if(element.name == "Timothy"){
element.belongings[0] = "Rusty Sword"
}
});
console.log(party);
// Prompt 11: Write a function called setLeader that takes a name as a parameter.
// The member with that name should have a new property leader: true while the other members have leader: false.
const setLeader = (name) => {
party.forEach(element => {
if (element.name == name) {
element.leader = true;
party.forEach(element => {
if (element.name !== name) {
element.leader = false;
}
})
}
})
}
setLeader("Sarah");
console.log(party);