-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
203 lines (155 loc) · 4.02 KB
/
main.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
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
function doubleAll(numbers) {
const doubled = [];
for (let i = 0; i < numbers.length; i++) {
doubled.push(numbers[i] * 2);
}
return doubled;
}
function yelledGreetings(greetings) {
const yelled = [];
for (let i = 0; i < greetings.length; i++) {
yelled.push(greetings[i] + '!')
}
return yelled;
}
function absoluteValues(numbers) {
const absolutes = [];
for (let i = 0; i < numbers.length; i++) {
const number = numbers[i]
if (number < 0) {
absolutes.push(number * -1);
} else {
absolutes.push(number);
}
}
return absolutes;
}
// or, instead of an if/else, we could use Math.abs to get the absolute value:
function absoluteValues2(numbers) {
const absolutes = [];
for (let i = 0; i < numbers.length; i++) {
absolutes.push(Math.abs(numbers[i]));
}
return absolutes;
}
function upperCaseFirstLetters(names) {
let uppercasedNames = [];
for (let i = 0; i < names.length; i++) {
uppercasedNames.push(names[i][0].toUpperCase() + names[i].slice(1).toLowerCase())
}
return uppercasedNames;
}
// intermediate variables version
function changeToInitials(names) {
const initialsList = [];
for (let i = 0; i < names.length; i++) {
const name = names[i];
const initial1 = name[0];
const spaceIndex = name.indexOf(' ');
const initial2 = name[spaceIndex + 1];
initialsList.push(initial1 + initial2);
}
return initialsList;
}
// with a crazy one-liner instead of intermediate variables
function changeToInitials2(names) {
const initialsList = [];
for (let i = 0; i < names.length; i++) {
initialsList.push(names[i][0] + names[i][names[i].indexOf(' ') + 1]);
}
return initialsList;
}
// internal for loop version (string building)
function changeToInitials3(names) {
const initialsList = [];
for (let i = 0; i < names.length; i++) {
let initials = '';
for (let i = 0; i < name.length; i++) {
if (i === 0 || name[i - 1] === ' ') {
initials = initials + name[i].toUpperCase();
}
}
initialsList.push(initials)
}
return initialsList;
}
// if version
function doubleOdd(numbers) {
const answers = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 1 || numbers[i] % 2 === -1) {
answers.push(numbers[i] * 2)
} else {
answers.push(numbers[i])
}
}
return answers;
}
// if and re-assignment version:
function doubleOdd2(numbers) {
const answers = [];
for (let i = 0; i < numbers.length; i++) {
let answer = numbers[i];
if (answer % 2 === 1 || answer % 2 === -1) {
answer = answer * 2;
}
answers.push(answer);
}
return answers;
}
// Math.abs (absolute value) version
function doubleOdd3(numbers) {
const answers = [];
for (let i = 0; i < numbers.length; i++) {
if (Math.abs(numbers[i]) % 2 === 1) {
answers.push(numbers[i] * 2);
} else {
answers.push(numbers[i]);
}
}
return answers;
}
function add1ToLeft(numbers) {
const with1Addeds = [];
for (let i = 0; i < numbers.length; i++) {
const stringifiedNumber = numbers[i].toString();
if (numbers[i] < 0) {
const stringWith1Added = '-1' + stringifiedNumber.slice(1)
with1Addeds.push(Number(stringWith1Added));
} else {
const stringWith1Added = '1' + stringifiedNumber;
with1Addeds.push(Number(stringWith1Added));
}
}
return with1Addeds;
}
if (typeof doubleAll === 'undefined') {
doubleAll = undefined;
}
if (typeof yelledGreetings === 'undefined') {
yelledGreetings = undefined;
}
if (typeof absoluteValues === 'undefined') {
absoluteValues = undefined;
}
if (typeof upperCaseFirstLetters === 'undefined') {
upperCaseFirstLetters = undefined;
}
if (typeof changeToInitials === 'undefined') {
changeToInitials = undefined;
}
if (typeof doubleOdd === 'undefined') {
doubleOdd = undefined;
}
if (typeof add1ToLeft === 'undefined') {
add1ToLeft = undefined;
}
module.exports = {
doubleAll,
yelledGreetings,
absoluteValues,
upperCaseFirstLetters,
changeToInitials,
doubleOdd,
add1ToLeft,
}