forked from alex-aaron/function-master-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-master.js
More file actions
262 lines (211 loc) · 9.03 KB
/
Copy pathfunction-master.js
File metadata and controls
262 lines (211 loc) · 9.03 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//////////////////////////////////////////////////////////////////////
// Function 1 - Object Values ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//instructions in function-master.html on
function objectValues(object) {
var array = []
for (key in object){
return Object.values(object)
}
}
//////////////////////////////////////////////////////////////////////
// Function 2 - Keys to String ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function keysToString(object) {
//turn keys to string
//user array.join('')
var keyArray = Object.keys(object)
var joinedkeys = keyArray.join(' ')
return joinedkeys
}
//////////////////////////////////////////////////////////////////////
// Function 3 - Values to String /////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function valuesToString(object) {
var valuesArray = Object.values(object)
var arrayOfStrings = []
for (var i = 0; i < valuesArray.length; i++) {
if (typeof valuesArray[i] === 'string') {
arrayOfStrings.push(valuesArray[i])
}
}
var joined = arrayOfStrings.join(' ')
return joined
}
//////////////////////////////////////////////////////////////////////
// Function 4 - Array or Object //////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function arrayOrObject(collection) {
//if the value is an object return true
if (Array.isArray(collection)) {
return 'array'
}
//otherwise if the value is an array return false
else if (!Array.isArray(collection) && collection !== null && collection instanceof Date !== true && typeof collection !== undefined && typeof collection !== 'string' && typeof collection !== 'number' && typeof collection !== 'boolean')
{
return 'object'
}
//if all else fails return true
}
//////////////////////////////////////////////////////////////////////
// Function 5 - Capitalize Word //////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function capitalizeWord(string) {
var char = string.charAt(0)
var cap = char.toUpperCase()
var otherLet = string.slice(1)
return cap + otherLet
}
//////////////////////////////////////////////////////////////////////
// Function 6 - Capitalize All Words /////////////////////////////////
//////////////////////////////////////////////////////////////////////
//Should take a string of words and return a string with all the words capitalized
//("one two three four"), "One Two Three Four")
function capitalizeAllWords(string) {
var word = string.split(' ')
for (var i = 0; i < word.length; i++){
word[i] = word[i][0].toUpperCase() + word[i].substring(1)
var joined = word.join(' ')
}
return joined
}
` `
console.log(capitalizeAllWords("one two three four"))
//////////////////////////////////////////////////////////////////////
// Function 7 - Welcome Message //////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function welcomeMessage(object) {
var char = object.name.charAt(0)
var cap = char.toUpperCase()
var otherLet = object.name.slice(1)
return 'Welcome ' + cap + otherLet + '!'
}
//////////////////////////////////////////////////////////////////////
// Function 8 - Profile Info /////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function profileInfo(object) {
var char = object.name.charAt(0)
var cap = char.toUpperCase()
var otherLet = object.name.slice(1)
var charS = object.species.charAt(0)
var capS = charS.toUpperCase()
var otherLetS = object.species.slice(1)
return cap + otherLet + ' is a ' + capS + otherLetS
}
//////////////////////////////////////////////////////////////////////
// Function 9 - Maybe Noises /////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function maybeNoises(object) {
if ('noises' in object === true && object.noises.length > 0) {
var joinedNoises = object.noises.join(' ')
return joinedNoises
}
else {
return 'there are no noises'
}
}
//////////////////////////////////////////////////////////////////////
// Function 10 - Has Words ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function hasWord(string, word) {
//Should take a string of words and a word and return true if <word> is in <string of words>, otherwise return false
if (string.includes(word)){
return true
} else {
return false
}
}
//////////////////////////////////////////////////////////////////////
// Function 11 - Add Friend //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function addFriend (name, object) {
//Should take a name and an object and add the name to the object's friends array then return the object
for (var key in object) {
object.friends.push(name)
}
return object
}
//////////////////////////////////////////////////////////////////////
// Function 12 - Is Friend ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function isFriend(name, object) {
//Should take a name and an object and return true if <name> is a friend of <object> and false otherwise
if (object.hasOwnProperty('friends') && object.friends.includes(name)) {
return true
} else {
return false
}
}
//////////////////////////////////////////////////////////////////////
// Function 13 - Non-Friends /////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function nonFriends(name, array) {
//Should take a name and a list of people, and return a list of all the names that <name> is not friends with
/* var data = [
{name: "Jimmy", friends:["Sara", "Liza"]},
{name: "Bob", friends:[]},
{name: "Liza", friends: ["Jimmy"]},
{name: "Sara", friends: ["Jimmy"]}
]; */
var notFriends = []
for (var i = 0; i < array.length; i++) {
var friendsArray = array[i].friends
if (name !== array[i].name && !friendsArray.includes(name)){
notFriends.push(array[i].name)
}
} return notFriends
}
//////////////////////////////////////////////////////////////////////
// Function 14 - Update Object ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function updateObject(object, key, value) {
//Should take an object, a key and a value. Should update the property <key> on <object> with new <value>. If <key> does not exist on <object> create it.
object[key] = value
return object
}
//////////////////////////////////////////////////////////////////////
// Function 15 - Remove Properties ///////////////////////////////////
//////////////////////////////////////////////////////////////////////
function removeProperties(object, array) {
// Should take an object and an array of strings. Should remove any properties on <object> that are listed in <array>
for (var i = 0; i < array.length; i++){
delete object[array[i]]
}
return object
}
//////////////////////////////////////////////////////////////////////
// Function 16 - Dedup ///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
function dedup(array) {
//Should take an array and return an array with all the duplicates removed
var notDuplicate = []
for (var i = 0; i < array.length; i++){
if (!notDuplicate.includes(array[i])) {
notDuplicate.push(array[i])
}
}
return notDuplicate
}
//////////////////////////////////////////////////////////////////////
// DON'T REMOVE THIS CODE ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
if((typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')) {
// here, export any references you need for tests //
module.exports.objectValues = objectValues;
module.exports.keysToString = keysToString;
module.exports.valuesToString = valuesToString;
module.exports.arrayOrObject = arrayOrObject;
module.exports.capitalizeWord = capitalizeWord;
module.exports.capitalizeAllWords = capitalizeAllWords;
module.exports.welcomeMessage = welcomeMessage;
module.exports.profileInfo = profileInfo;
module.exports.maybeNoises = maybeNoises;
module.exports.hasWord = hasWord;
module.exports.addFriend = addFriend;
module.exports.isFriend = isFriend;
module.exports.nonFriends = nonFriends;
module.exports.updateObject = updateObject;
module.exports.removeProperties = removeProperties;
module.exports.dedup = dedup;
}