forked from bloominstituteoftechnology/Basic-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-2.js
More file actions
171 lines (153 loc) · 2.87 KB
/
project-2.js
File metadata and controls
171 lines (153 loc) · 2.87 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
const getBiggest = (x, y) => {
if (x > y) {
return x;
} else if (x === y) {
return y;
}
return y;
};
const greeting = (language) => {
switch (language) {
case 'German':
return 'Guten Tag!';
case 'Spanish':
return 'Hola!';
case 'Chinese':
return 'Ni Hao!';
default:
return 'Hello!';
}
};
const isTenOrFive = (num) => {
if (num === 10 || num === 5) {
return true;
}
return false;
};
const isInRange = (num) => {
if (num < 50 && num > 20) {
return true;
}
return false;
};
const isInteger = (num) => {
if (Math.floor(num) === num) {
return true;
}
return false;
};
const fizzBuzz = (num) => {
if (num % 5 === 0 && num % 3 === 0) {
return 'fizzbuzz';
} else if (num % 5 === 0) {
return 'buzz';
} else if (num % 3 === 0) {
return 'fizz';
}
return num;
};
const isPrime = (num) => {
if (num < 0) {
return false;
}
if (num === 1 || num === 0) {
return false;
}
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
};
const returnFirst = (arr) => {
return arr[0];
};
const returnLast = (arr) => {
return arr[arr.length - 1];
};
const getArrayLength = (arr) => {
return arr.length;
};
const incrementByOne = (arr) => {
for (let i = 0; i < arr.length; i++) {
arr[i]++;
}
return arr;
};
const addItemToArray = (arr, item) => {
arr.push(item);
return arr;
};
const addItemToFront = (arr, item) => {
arr.unshift(item);
return arr;
};
const wordsToSentence = (words) => {
let newSentence = '';
for (let i = 0; i < words.length; i++) {
if (i === 0) {
newSentence += `${words[i]}`;
} else {
newSentence += ` ${words[i]}`;
}
}
return newSentence;
};
const contains = (arr, item) => {
let itemCounter = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
itemCounter++;
}
}
if (itemCounter > 0) {
return true;
}
return false;
};
const addNumbers = (numbers) => {
let sumOfNumbers = 0;
for (let i = 0; i < numbers.length; i++) {
sumOfNumbers += numbers[i];
}
return sumOfNumbers;
};
const averageTestScore = (testScores) => {
let totalSumScores = 0;
let numberOfScore = 0;
for (let i = 0; i < testScores.length; i++) {
totalSumScores += testScores[i];
numberOfScore++;
}
return totalSumScores / numberOfScore;
};
const largestNumber = (numbers) => {
let biggestInteger = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > biggestInteger) {
biggestInteger = numbers[i];
}
}
return biggestInteger;
};
module.exports = {
getBiggest,
greeting,
isTenOrFive,
isInRange,
isInteger,
fizzBuzz,
isPrime,
returnFirst,
returnLast,
getArrayLength,
incrementByOne,
addItemToArray,
addItemToFront,
wordsToSentence,
contains,
addNumbers,
averageTestScore,
largestNumber
};