forked from JoinCODED/PreCourse-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.js
More file actions
223 lines (212 loc) · 5.2 KB
/
project.js
File metadata and controls
223 lines (212 loc) · 5.2 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
/**
* REFERENCES:
* - String methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#
*
* If you'd like to run your function to test it yourself,
* uncomment it from the bottom of this file, then run this
* file with `node project.js`.
*
* Good luck!
*/
/**
* sumOdds(numbers):
* - receives an array of numbers
* - returns the sum of only the ODD numbers
*
* e.g.
* sumOdds([1, 2, 3, 4, 5, 6, 7, 8, 9]) -> 25
* sumOdds([3, 7, 8, 15, 2, 1, 13]) -> 39
*/
function sumOdds(numbers) {
// Your code here
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 !== 0) { sum = sum + numbers[i]; }
}
return sum;
}
/**
* characterCount(string, c):
* - receives a string and a character
* - returns the number of times `c` occurs in the string
* - note: this has to be case-insensitive
*
* e.g.
* characterCount("Michael Stephenson", "e") -> 3
* characterCount("Character Count is clever", "c") -> 4
*
* Hint: Use string methods to make it case-insensitive
*/
function characterCount(string, c) {
// Your code here
let sum = 0;
for (i = 0; i < string.length; i++) {
if (string[i] === c.toUpperCase() || string[i] === c.toLowerCase()) { sum = sum + 1; }
}
return sum;
}
/**
* differences(numbers):
* - receives an array of numbers
* - returns an array that has the difference in the values of the numbers array.
* - see example below for clarification.
*
* e.g.
* differences([1, 3, 7, 9, 12]) -> [2, 4, 2, 3]
* 3 - 1 = 2
* 7 - 3 = 4
* 9 - 7 = 2
* 12 - 9 = 3
*
* differences([11, 35, 52, 14, 56]) -> [24, 17, -38, 42]
*/
function differences(numbers) {
// Your code here
var New = [];
for (let i = 0; i < numbers.length - 1; i++) {
New.push(numbers[i + 1] - numbers[i]);
}
return New;
}
/**
* largestIncrement(numbers):
* - receives an array of numbers
* - returns the largest difference between two consecutive numbers in the array
* - see example below for clarification
*
* e.g.
* largestIncrement([1, 3, 7, 9, 12]) -> 4
* 3 - 1 = 2
* 7 - 3 = 4
* 9 - 7 = 2
* 12 - 9 = 3
* largest: 4
*
* largestIncrement([11, 35, 52, 14, 56, 601, 777, 888, 999]) -> 545
*/
function largestIncrement(numbers) {
// Your code here
let diff = 0;
let temp = 0;
for (let i = 0; i < numbers.length - 1; i++) {
temp = numbers[i + 1] - numbers[i];
if (temp > diff) {
diff = temp;
}
}
return diff;
}
/**
* afterX(numbers, x):
* - receives an array of numbers, and a number `x`.
* - returns an array of every number in `numbers` that occurs after `x`.
*
* - assume there are no duplicate numbers.
*
* e.g.
* afterX([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) -> [4, 5, 6, 7, 8, 9]
* afterX([11, 35, 52, 14, 56, 601, 777, 888, 999], 52) -> [14, 56, 601, 777, 888, 999]
*/
function afterX(numbers, x) {
// Your code here
var New = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] == x) {
New = numbers.slice(i + 1, numbers.length);
}
}
return New;
}
/**
* abbreviate(firstName, lastName):
* - receives a first name and a last name
* - returns the initials in uppercase
*
* e.g.
* abbreviate("Michael", "Singer") -> "MS"
* abbreviate("jordan", "peterson") -> "JP"
*
* Hint: Use string method .toUpperCase()
*/
function abbreviate(firstName, lastName) {
// Your code here
var New = [];
New[0] = firstName[0].toUpperCase();
New[1] = lastName[0].toUpperCase();
New = New.join(``);
return New;
}
/**
* isUpperCase(string):
* - receives a string
* - returns true if the string is uppercase, false otherwise
*
* e.g.
* isUpperCase("Mickey S") -> false
* isUpperCase("JCREW") -> true
*
*/
function isUpperCase(string) {
// Your code here
if (string === string.toUpperCase()) { return true; }
else { return false }
}
/**
* elementInArray(numbers, x):
* - receives an array of numbers, and a number `x`.
* - returns true if `x` is found in the array, false otherwise
*
* e.g.
* elementInArray([5, 6, 7], 6) -> true
* elementInArray([5, 6, 7], 8) -> false
*
*/
function elementInArray(numbers, x) {
// Your code here
let New;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === x) { New = false; break; }
else { New = true; break; };
}
return New;
}
/**
* reverseString(string):
* - receives a string
* - returns the reverse of the string
*
* e.g.
* reverseString("string") -> "gnirts"
* reverseString("CODED") -> "DEDOC"
*
*/
function reverseString(string) {
// Your code here
let New = [];
for (let i = 0; i < string.length; i++) {
New.push(string[i]);
}
New = New.reverse();
New = New.join(``);
return New;
}
//console.log(sumOdds([3, 7, 8, 15, 2, 1, 13]));
//console.log(characterCount("Character Count is clever", "c"));
//console.log(differences([11, 35, 52, 14, 56]));
//console.log(largestIncrement([11, 35, 52, 14, 56, 601, 777, 888, 999]));
//console.log(afterX([1, 2, 3, 4, 5, 6, 7, 8, 9], 3));
//console.log(abbreviate("miss", "Stephane"));
//console.log(isUpperCase("JCREW"));
//console.log(elementInArray([5, 6, 7], 7));
//console.log(reverseString("CODED"));
module.exports = {
sumOdds,
characterCount,
differences,
largestIncrement,
afterX,
abbreviate,
isUpperCase,
elementInArray,
reverseString,
};