-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththird-assesment-practice.html
220 lines (200 loc) · 7.86 KB
/
third-assesment-practice.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// # Third JavaScript Assessment Practice Problems
//
// The third JavaScript assessment will require the writing of functions that manipulate various data types, including arrays and objects. The use of iteration (loops) in many cases will be required to achieve this. The second halve of the JavaScript 101 exercises is excellent preparation for the assessment problems. This is a resource for additional practice problems.
//
// ## Problems
//
// 1. Create a function, `printAll`, that takes an array and logs every element on a new line in the console.
// function printAll(userArray){
// userArray.forEach(item =>{
// console.log(item);
// })
// }
//
//
//
// // ```javascript
// printAll(['hello', 'hi', 'greetings']);
// /*
// prints...
// hello
// hi
// greetings
// */
// ```
//
// 1. Create a function, `getLowestNumber`, that take in an array of numbers and returns the lowest number.
// function getLowestNumber(arr){
// let min = Math.min.apply(Math, arr)
// console.log(min)
// }
//
// // ```javascript
// getLowestNumber([23,47,50,5]);
// // returns 5
// getLowestNumber([5.8,7.3,8.2,4.7, 4.3]);
// // returns 4.3
// getLowestNumber([-7,9,76,0,-4]);
// returns -7
// ```
//
// 1. Create a function, `getOccurrences`, that that takes in two arguments, a string and a letter. The function will count the number of occurrences of the specified letter within the string and return that number. The function should recognize case for instances (e.g. 'M' does not equal 'm').
// let commonLetters = 0
// function getOccurrences(myString, letter){
//
// for(let i=0; i<myString.length; commonLetters+=+(letter===myString[i++])){
//
// if(letter === character){
// commonLetters++
// }
// return commonLetters;
// })
// for(var i=commonLetters=0; i<myString.length; commonLetters+=+(letter===myString[i++]));
// }
// function getOccurrences(myString, letter){
// let commonLetters = 0;
// for(let i=commonLetters=0; i<myString.length; commonLetters+=(letter===myString[i++]));
// return commonLetters;
// }
function getOccurrences(myString, letter){
let commonLetters = 0;
for(var i=0; i<myString.length; i++) {
commonLetters+=(letter===myString[i])
}
return commonLetters;
}
console.log(getOccurrences('hello', 'l'));
// returns 2
console.log(getOccurrences('mississippi', 's'));
// returns 4
console.log(getOccurrences('Bubble', 'B'));
// returns 1
// ```
//
// 1. Create a function, `getLongestString`, that takes in an array of strings and returns the longest string. If the two longest words are equal in length, return the last to appear in the array.
function getLongestString(arr){
let index = 0;
for(let i =0; i < arr.length; i++){
}
}
// ```javascript
// getLongestString(['hello', 'hi', 'greetings']); // returns 'greetings'
// getLongestString(['hello', 'world', '!' ]); // returns 'world'
// ```
//
// 1. Create a function, `getFirstLetter`, that takes an array of strings and returns an array of the first letter of each string.
//
// ```javascript
// getFirstLetter(['hello', 'hi', 'greetings']); // returns ['h','h','g']
// getFirstLetter(['hello', 'world', '!' ]); // returns ['h','w','!']
// ```
//
// 1. Create a function, `arrayEndsWith`, that takes two arguments, an array and a shorter array, and returns a boolean whether or not the larger array ends with the same elements as the passed second array elements. The function should return true if the second array elements are at the end of the first array. Assume that neither array will be empty, contain only string, number, or boolean elements and that the length of the second array will always be shorter than the first.
function arrayEndsWith(long, short) {
let commonLetters = 0;
for (let i=0; i<short.length; i++) {
if (long[(long.length-short.length)+i]===short[i]) {
console.log(long.length)
commonLetters++;
}
}
return commonLetters===short.length;
}
// ```javascript
console.log(arrayEndsWith(['hi', 'hello', 'world', true, 3, 5], [3, 5]));
// returns true
// arrayEndsWith(['hi', 'hello', 'world', true, 3, 5], [3, 9]); // returns false
// arrayEndsWith(['hi', 'hmmm'], ['hmmm']); // returns true
// arrayEndsWith([1, 2, 3], [2, 3]); // returns true
// arrayEndsWith([1, 2, 3], [3, 2]); // returns false
// ```
//
// 1. Create a function, `numsToObject`, that takes in three number inputs and returns them as property values `num1`, `num2`, and `num3` on an object.
//
// ```javascript
// numsToObject(4, 5, 6); // returns {num1: 4, num2: 5, num3: 6}
// numsToObject(1, 2, 3); // returns {num1: 1, num2: 2, num3: 3}
// numsToObject(0, 199, 34); // returns {num1: 0, num2: 199, num3: 34}
// ```
//
// 1. Create a function, `removeNums`, that takes in an array of data types and returns an array of the same elements except for any number data types. Numeric strings do not count as a number but NaN does.
//
// ```javascript
// removeNums(['a', true, null, [], {}, 4, '5', NaN]) // returns ['a', true, null, [], {}, '5']
// removeNums(['a', 'b', 'c']) // returns ['a', true, null, [], {}, '5']
// ```
//
// 1. Create a function, `objectToSum`, that takes in an object, and returns the sum of any number property values (numeric strings will not be added). Expect that no object property values will be NaN. If no number properties are present, return 0.
function objectToSum(obj) {
var objArr = Object.values(obj);
var newArr = 0;
objArr.forEach(function (arr){
if (typeof arr !== "number") {
newArr += 0;
} else {
newArr += arr;
}
});
return newArr;
}
// ```javascript
// objectToSum({prop1: 'bob', prop2: true, prop3: 5}) // returns 5
// objectToSum({a: '3', b: true, c: 5, d: 3}) // returns 8
// objectToSum({foo: 'one', bar: 'two'}) // returns 0
// ```
//
// // 1. Create a function, `objToObj`, that takes in an object with only property values of a string type and returns another object with a single property called 'all' with a value of all input object properties values concatenated together.
// function objToObj(obj){
// let all = Object.values(obj);
// let string = '';
// all.forEach(element => {
// string += element;
// });
// return{all: string};
// }
// ***Please note that the order of object properties is uncertain and concatenating the object property values will be unpredictable. This is fine.***
//
// ```javascript
// console.log(objToObj({foo: 'hello', bar: 'world'}));
// returns {all: 'helloworld'} or {all: 'worldhello'}
// console.log(objToObj({a: 'codeup', b: 'rocks'}));
// returns {all: 'rockscodeup'} or {all: 'codeuprocks'}
// ```
//
// 1. Create a function, `getStringDeets`, that takes in a string and returns an object with specific properties containing information about the string, namely:
//
// - `firstChar` - containing the first character of the string
// - `lastChar` - containing the last character of the string
// - `length`- containing the length of characters of the string
// - `shoutedVersion` - containing an all caps version of the string
//
// ```javascript
// getStringDeets("apple"); // returns...
// /*
// {
// firstChar: "a",
// lastChar: "e",
// length: 5,
// shoutedVersion: "APPLE"
// }
// */
// ```
//
// 1. Create a function, `createUsersObject`, that takes in two arrays: the first array is an array of strings (usernames), the second is an array of numbers (user ages). The function should return an object with property names matching to the first array elements paired with property values matching the second array elements. Assume both arrays are the same length.
//
// ```javascript
// var usernames = ['cindy', 'fred', 'cathy'];
// var ages = [34, 22, 45];
// createUsersObject(usernames, ages) // returns {cindy: 34, fred: 22, cathy: 45}
// ```
</script>
</body>
</html>