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
119 lines (115 loc) · 3.08 KB
/
arrayFunctions.js
File metadata and controls
119 lines (115 loc) · 3.08 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
/**
* 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
*/
function isArrayLengthOdd(numbers) {
if (numbers.length % 2 == 0) {
return false;
} else {
return true;
}
}
let arrayOdd = [1, 2, 3, 4];
console.log(isArrayLengthOdd(arrayOdd));
/**
* 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
*/
function isArrayLengthEven(numbers) {
if (numbers.length % 2 == 0) {
return true;
} else {
return false;
}
}
let arrayEven = [1, 2, 3, 4];
console.log(isArrayLengthEven(arrayEven));
/**
* 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"]
*/
function addLailaToArray(instructor) {
// Your code here
instructor.push("Laila");
return instructor;
}
let instructors = ["Mshary", "Hassan"];
addLailaToArray(instructors);
console.log(addLailaToArray(["Mshary", "Hasan"]));
/**
* eliminateTeam(teams):
* - receives array `teams`
* - removes the last element from the array and return it
*
* e.g.
* eliminateTeam(["Brazil", "Germany", "Italy"]) -> "Italy"
*/
function eliminateTeam(teams) {
return teams.pop();
}
let teams = ["Brazil", "Germany", "Italy"];
//eliminateTeam(teams);
console.log(eliminateTeam(teams));
/**
* 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) {
// Your code here
if (fruits.length % 2 == 0) {
let half = fruits.length / 2;
return fruits.slice(half);
} else {
fruits.splice(0, fruits.length);
return fruits;
}
}
let array12 = [1, 2, 3, 4];
// secondHalfOfArrayIfItIsEven(array12);
console.log(secondHalfOfArrayIfItIsEven(array12));
/**
* 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 number method .indexOf()
* - Use string method .slice()
*/
function youGottaCalmDown(shout) {
let index = shout.indexOf("!");
if (index == -1) {
return shout;
} else {
return shout.slice(0, index + 1);
}
}
let shout = "HI!!!!!!!!!!";
console.log(youGottaCalmDown(shout));