forked from JoinCODED/TASK-JS-Arrays
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayFunctions.js
More file actions
136 lines (113 loc) · 3.23 KB
/
arrayFunctions.js
File metadata and controls
136 lines (113 loc) · 3.23 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
/**
* isArrayLengthOdd(numbers):
* - receives array `numbers`
* - returns true if array has an odd number of elements
* - returns false otherwise
*
* e.g.
* isArrayLengthOdd([1, 2, 3]) -> true
* isArrayLengthOdd([1, 2, 3, 4]) -> flase
*/
console.log(isArrayLengthOdd([1, 2, 3]));
console.log(isArrayLengthOdd([1, 2, 3, 4]));
function isArrayLengthOdd(numbers) {
if(numbers.length%2 !== 0){
return true;
}else
return false;
}
/**
* isArrayLengthEven(numbers):
* - receives array `numbers`
* - returns true if array has an even number of elements
* - returns false otherwise
*
* e.g.
* isArrayLengthEven([1, 2, 3]) -> false
* isArrayLengthEven([1, 2, 3, 4]) -> true
*/
console.log(isArrayLengthEven([1, 2, 3]));
console.log(isArrayLengthEven([1, 2, 3, 4]));
function isArrayLengthEven(numbers) {
if(numbers.length%2 == 0){
return true;
}
return false;
}
/**
* addLailaToArray(instructors):
* - receives array `instructors`
* - returns a new array that's a copy of array `instructors` with additional string "Laila"
*
* e.g.
* addLailaToArray(["Mshary", "Hasan"]) -> ["Mshary", "Hasan", "Laila"]
*/
let instructors=["Mshary", "Hasan"];
function addLailaToArray(instructors) {
instructors.push("Laila");
return instructors;
}
/**
* eliminateTeam(teams):
* - receives array `teams`
* - removes the last element from the array and return it
*
* e.g.
* eliminateTeam(["Brazil", "Germany", "Italy"]) -> "Italy"
*/
let teams=["Brazil", "Germany", "Italy"];
function eliminateTeam(teams) {
return teams.pop();
}
/**
* secondHalfOfArrayIfItIsEven(fruits):
* - receives array `fruits`
* - returns a new array that's the second half of the original array if it has an even number of elements
* - returns an empty array if it has an odd number of elements
*
* e.g.
* secondHalfOfArrayIfItIsEven(["apple", "orange", "banana", "kiwi"]) -> ["banana", "kiwi"]
* secondHalfOfArrayIfItIsEven(["apple", "orange", "banana", "kiwi", "blueberry"]) -> []
*/
function secondHalfOfArrayIfItIsEven(fruits) {
if(fruits.length%2 == 0){
fruits=fruits.slice(parseInt(fruits.length/2),fruits.length);
}else if(fruits.length%2 !== 0){
fruits=fruits.slice(0,0);
}
return fruits;
}
console.log(secondHalfOfArrayIfItIsEven(["apple", "orange", "banana", "kiwi"]));
console.log(secondHalfOfArrayIfItIsEven(["apple", "orange", "banana", "kiwi", "blueberry"]));
/**
* youGottaCalmDown(shout):
* - receives a string `shout`
* - returns the string `shout` 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()
*/
// console.log(youGottaCalmDown("Taylor Schwifting!!!!!!!!!!!"));
// console.log(youGottaCalmDown("Hellooooo"));
function youGottaCalmDown(shout) {
if(shout.endsWith("!")){
let index=shout.indexOf("!");
return shout.slice(0,index+1);
}
return shout;
}
console.log(youGottaCalmDown("HI!!!!!!!!!!"));
module.exports = {
isArrayLengthOdd,
isArrayLengthEven,
addLailaToArray,
eliminateTeam,
secondHalfOfArrayIfItIsEven,
youGottaCalmDown,
};