forked from SunJieMing/js-minicamp-homework-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.js
217 lines (194 loc) · 4.78 KB
/
exercises.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
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
// Do not change any of the function names
function getBiggest(x, y) {
// x and y are integers. Return the larger integer
// if they are the same return either one
if(x > y){
return x;
}
return y;
}
function greeting(language) {
// return a greeting for three different languages:
// language: 'German' -> 'Guten Tag!'
// language: 'English' -> 'Hello!'
// language: 'Spanish' -> 'Hola!'
// if language is undefined return 'Hello!'
if(language === 'German'){
return 'Guten Tag!';
}
if(language === 'English'){
return 'Hello!';
}
if(language === 'Spanish'){
return 'Hola!';
}
return 'Hello!';
}
function isTenOrFive(num) {
// return true if num is 10 or 5
// otherwise return false
if(num === 10 || num === 5){
return true;
}
return false;
}
function isInRange(num) {
// return true if num is less than 50 and greater than 20
if(num < 50 && num > 20){
return true;
}
return false;
}
function isInteger(num) {
// return true if num is an integer
// 0.8 -> false
// 1 -> true
// -10 -> true
// otherwise return false
// hint: you can solve this using Math.floor
if(Math.floor(num) === num){
return true;
}
return false;
}
function fizzBuzz(num) {
// if num is divisible by 3 return 'fizz'
// if num is divisible by 5 return 'buzz'
// if num is divisible by 3 & 5 return 'fizzbuzz'
// otherwise return num
var numToWord = '';
var isDivisible = false;
if(num % 3 === 0){
numToWord = numToWord + 'fizz';
isDivisible = true;
}
if(num % 5 === 0){
numToWord = numToWord + 'buzz';
isDivisible = true;
}
if(isDivisible){
return numToWord;
}
return num;
}
function isPrime(num) {
// return true if num is prime.
// otherwise return false
// hint: a prime number is only evenly divisible by itself and 1
// hint2: you can solve this using a for loop
// note: 0 and 1 are NOT considered prime numbers
for(var i = num - 1; i > 1; i--){
if(num % i === 0){
return false;
}
}
if(num === 0 || num === 1){
return false;
}
return true;
}
function returnFirst(arr) {
// return the first item from the array
return arr[0];
}
function returnLast(arr) {
// return the last item of the array
return arr[arr.length - 1];
}
function getArrayLength(arr) {
// return the length of the array
return arr.length;
}
function incrementByOne(arr) {
// arr is an array of integers
// increase each integer by one
// return the array
for(var i = 0; i < arr.length; i++){
arr[i] += 1;
}
return arr;
}
function addItemToArray(arr, item) {
// add the item to the end of the array
// return the array
arr.push(item);
return arr;
}
function addItemToFront(arr, item) {
// add the item to the front of the array
// return the array
// hint: use the array method .unshift
arr.unshift(item);
return arr;
}
function wordsToSentence(words) {
// words is an array of strings
// return a string that is all of the words concatenated together
// spaces need to be between each word
// example: ['Hello', 'world!'] -> 'Hello world!'
var sentence = '';
for(var i = 0; i < words.length - 1; i++){
sentence = sentence + words[i] + ' ';
}
sentence = sentence + words[words.length - 1];
return sentence;
}
function contains(arr, item) {
// check to see if item is inside of arr
// return true if it is, otherwise return false
if(arr.includes(item)){
return true;
}
return false;
}
function addNumbers(numbers) {
// numbers is an array of integers.
// add all of the integers and return the value
var sum = 0;
for(var i = 0; i < numbers.length; i++){
sum += numbers[i];
}
return sum;
}
function averageTestScore(testScores) {
// testScores is an array. Iterate over testScores and compute the average.
// return the average
var sum = 0;
for(var i = 0; i < testScores.length; i++){
sum += testScores[i];
}
return sum / testScores.length;
}
function largestNumber(numbers) {
// numbers is an array of integers
// return the largest integer
var currentLargest = 0;
for(var i = 0; i < numbers.length; i++){
if(numbers[i] > currentLargest){
currentLargest = numbers[i];
}
}
return currentLargest;
}
// Do not modify code below this line.
// --------------------------------
module.exports = {
getBiggest: getBiggest,
greeting: greeting,
isTenOrFive: isTenOrFive,
isInRange: isInRange,
isInteger: isInteger,
fizzBuzz: fizzBuzz,
isPrime: isPrime,
returnFirst: returnFirst,
returnLast: returnLast,
getArrayLength: getArrayLength,
incrementByOne: incrementByOne,
addItemToArray: addItemToArray,
addItemToFront: addItemToFront,
wordsToSentence: wordsToSentence,
contains: contains,
addNumbers: addNumbers,
averageTestScore: averageTestScore,
largestNumber: largestNumber
};