This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy path1-create-functions.js
More file actions
190 lines (163 loc) · 4.24 KB
/
1-create-functions.js
File metadata and controls
190 lines (163 loc) · 4.24 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
/*
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the first five elements of the passed array.
*/
function first5(arr) {
for (let i =0; i < 5 ; i++ ){
return arr[i]
}
/*
Write a function that:
- Accepts an array as a parameter.
- Returns a new array containing the same elements, except sorted.
*/
function sortArray(arr) {
let arrSorted = arr.sort()
return arrSorted
}
/*
NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week.
Think about what is better about this solution than your one last week, and what is worse.
Write a function that:
- Takes an array of strings as input.
- Removes any spaces in the beginning or end each string.
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString(str) {
trimStr = str.trim();
mystring = trimStr.replace('/','');
lowerStr = mystring.toLowerCase()
}
/*
Write a function that:
- Takes an array and an index as input.
- Returns a new array containing the same elements, but without the element at the passed index.
*/
function remove(arr, index) {
arr.splice(index, 1);
return arr;
}
/*
Write a function that:
- Takes an array of numbers as input.
- Returns an array of strings formatted as percentages (e.g. 10 => "10%").
- The numbers must be rounded to 2 decimal places.
- Numbers greater 100 must be replaced with 100.
*/
/*function formatPercentage(arr) {
let arrD = []
let arrE = []
let arrB = arr.toString()
console.log(arrB)
let arrC = arrB.split(",")
console.log(arrC)
for (let i = 0; i < arrC.length; i++){
arrD.push(arrC[i] + "!")
}
console.log(arrD)
for (let num of arrD){
if (num.length > 3){
arrE.push('100%')
}
else{
arrE.push(num)
}
}
formatPercentage([1,2,1000,5,6,34])*/
function formatPercentage(arr) {
let result = [];
for (const number of arr) {
if (number > 100) {
result.push('100%')
} else {
result.push(`${Number(number.toFixed(2))}%`)
}
}
return result;
}
/* ======= TESTS - DO NOT MODIFY ===== */
test("first5 function works for more than five elements", () => {
const numbers = [1, 2, 3, 4, 5, 6, 7, 8];
const copyOfOriginal = numbers.slice();
expect(first5(numbers)).toEqual([1, 2, 3, 4, 5]);
// Make sure first5 didn't change its input array.
expect(numbers).toEqual(copyOfOriginal);
});
test("first5 function returns a a smaller array for fewer than five elements", () => {
const letters = ["z", "y", "x"];
const copyOfOriginal = letters.slice();
expect(first5(letters)).toEqual(["z", "y", "x"]);
// Make sure first5 didn't change its input array.
expect(letters).toEqual(copyOfOriginal);
});
test("sortArray function returns a sorted version of the array", () => {
expect(sortArray(["a", "n", "c", "e", "z", "f"])).toEqual([
"a",
"c",
"e",
"f",
"n",
"z",
]);
});
test("sortArray function doesn't change the passed in array", () => {
const before = ["a", "n", "c", "e", "z", "f"];
const copy = before.slice();
sortArray(before);
expect(before).toEqual(copy);
});
test("tidyUpString function works", () => {
expect(
tidyUpString([
"/Daniel",
" /Sanyia",
"AnTHonY",
"irina",
" Gordon",
"ashleigh ",
" Alastair ",
" anne marie ",
])
).toEqual([
"daniel",
"sanyia",
"anthony",
"irina",
"gordon",
"ashleigh",
"alastair",
"anne marie",
]);
});
describe("remove function", () => {
test("removes index 0", () => {
expect(remove([1, 2, 3], 0)).toEqual([2, 3]);
});
test("removes middle index", () => {
expect(remove([1, 2, 3, 4, 5], 2)).toEqual([1, 2, 4, 5]);
});
test("removes end index", () => {
expect(remove([1, 2, 3, 4, 5], 4)).toEqual([1, 2, 3, 4]);
});
test("removes only index", () => {
expect(remove(["hi"], 0)).toEqual([]);
});
test("ignores missing index", () => {
expect(remove(["hi"], 10)).toEqual(["hi"]);
});
test("doesn't modify input array", () => {
let initial = [1, 2, 3];
remove(initial, 1);
expect(initial).toEqual([1, 2, 3]);
});
});
test("formatPercentage function works", () => {
expect(formatPercentage([23, 18.103, 187.2, 0.372])).toEqual([
"23%",
"18.1%",
"100%",
"0.37%",
]);
});