forked from JoinCODED/JSFoundations-Arrays-Loops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayFunctions.js
83 lines (75 loc) · 2.11 KB
/
arrayFunctions.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
/**
* getOdds(numbers):
* - receives an array of numbers called `numbers`
* - filters the `numbers` array in order to...
* - returns an array of only ODD numbers.
*
* e.g.
* getOdds([1, 2, 3, 4, 5, 6, 7, 8, 9]) -> [1, 3, 5, 7, 9]
* getOdds([11, 35, 52, 14, 56, 601, 777, 888, 999]) -> [11, 35, 601, 777, 999]
*/
function getOdds(numbers) {
const newOdd = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 !== 0) {
newOdd.push(numbers[i]);
}
}
return newOdd;
}
/**
* getEvens(numbers):
* - receives an array of numbers called `numbers`
* - filters the `numbers` array in order to...
* - returns an array of only EVEN numbers.
*
* e.g.
* getEvens([1, 2, 3, 4, 5, 6, 7, 8, 9]) -> [2, 4, 6, 8]
* getEvens([11, 35, 52, 14, 56, 601, 777, 888, 999]) -> [52, 14, 56, 888]
*/
function getEvens(numbers) {
const newEven = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
newEven.push(numbers[i]);
}
}
return newEven;
}
/**
* getDuplicateCount(x, numbers):
* - receives a number `x`, and an array of numbers called `numbers`
* - returns the number of times `x` occurs in `numbers`.
*
* e.g.
* getDuplicateCount(1, [1, 2, 3, 1, 4, 5, 6, 1, 7, 8, 9, 10, 11, 1, 12, 13]) -> 4
* getDuplicateCount(52, [11, 35, 52, 14, 56, 601, 52, 777, 888, 999, 52]) -> 3
*/
function getDuplicateCount(x, numbers) {
let count = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === x) count++;
}
return count;
}
/**
* youGottaCalmDown(s):
* - receives a string `s`
* - returns the string `s` with at most one exclamation mark (!) at the end.
*
* e.g.
* youGottaCalmDown("HI!!!!!!!!!!") -> "HI!"
* youGottaCalmDown("Taylor Schwifting!!!!!!!!!!!") -> "Taylor Shwifting!"
* youGottaCalmDown("Hellooooo") -> "Hellooooo"
*
* Hint:
* - Use string method .slice()
* - Use string method .endsWith()
*/
function youGottaCalmDown(s) {
let numberex;
numberex = s.length;
while (s.endsWith("!", numberex - 1)) numberex = numberex - 1;
return s.slice(0, numberex);
}
module.exports = { getOdds, getEvens, getDuplicateCount, youGottaCalmDown };