Skip to content

Commit ac865e1

Browse files
committed
Completed Project 2
1 parent 8a322f0 commit ac865e1

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/project-2.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const getBiggest = (x, y) => {
44
// x and y are integers. Return the larger integer
55
// if they are the same return either one
6+
if (x >= y) {
7+
return x;
8+
} return y;
69
};
710

811
const greeting = (language) => {
@@ -11,15 +14,27 @@ const greeting = (language) => {
1114
// language: 'Spanish' -> 'Hola!'
1215
// language: 'Chinese' -> 'Ni Hao!'
1316
// if language is undefined return 'Hello!'
17+
switch (language) {
18+
case 'German':
19+
return 'Guten Tag!';
20+
case 'Chinese':
21+
return 'Ni Hao!';
22+
case 'Spanish':
23+
return 'Hola!';
24+
default:
25+
return 'Hello!';
26+
}
1427
};
1528

1629
const isTenOrFive = (num) => {
1730
// return true if num is 10 or 5
1831
// otherwise return false
32+
return num === 10 || num === 5;
1933
};
2034

2135
const isInRange = (num) => {
2236
// return true if num is less than 50 and greater than 20
37+
return num < 50 && num > 20;
2338
};
2439

2540
const isInteger = (num) => {
@@ -29,13 +44,21 @@ const isInteger = (num) => {
2944
// -10 -> true
3045
// otherwise return false
3146
// hint: you can solve this using Math.floor
47+
return num === Math.round(num);
3248
};
3349

3450
const fizzBuzz = (num) => {
3551
// if num is divisible by 3 return 'fizz'
3652
// if num is divisible by 5 return 'buzz'
3753
// if num is divisible by 3 & 5 return 'fizzbuzz'
3854
// otherwise return num
55+
if (num % 3 === 0 && num % 5 === 0) {
56+
return 'fizzbuzz';
57+
} else if (num % 3 === 0) {
58+
return 'fizz';
59+
} else if (num % 5 === 0) {
60+
return 'buzz';
61+
} return num;
3962
};
4063

4164
const isPrime = (num) => {
@@ -44,62 +67,99 @@ const isPrime = (num) => {
4467
// hint: a prime number is only evenly divisible by itself and 1
4568
// hint2: you can solve this using a for loop
4669
// note: 0 and 1 are NOT considered prime numbers
70+
if (num === 0 || num === 1) {
71+
return false;
72+
} for (let i = 2; i < num; num++) {
73+
if (num % i === 0) {
74+
return false;
75+
} return true;
76+
}
4777
};
4878

4979
const returnFirst = (arr) => {
5080
// return the first item from the array
81+
return arr[0];
5182
};
5283

5384
const returnLast = (arr) => {
5485
// return the last item of the array
86+
return arr[arr.length - 1];
5587
};
5688

5789
const getArrayLength = (arr) => {
5890
// return the length of the array
91+
return arr.length;
5992
};
6093

6194
const incrementByOne = (arr) => {
6295
// arr is an array of integers
6396
// increase each integer by one
6497
// return the array
98+
for (let i = 0; i < arr.length; i++) {
99+
arr[i] += 1;
100+
} return arr;
65101
};
66102

67103
const addItemToArray = (arr, item) => {
68104
// add the item to the end of the array
69105
// return the array
106+
arr.push(item);
107+
return arr;
70108
};
71109

72110
const addItemToFront = (arr, item) => {
73111
// add the item to the front of the array
74112
// return the array
75113
// hint: use the array method .unshift
114+
arr.unshift(item);
115+
return arr;
76116
};
77117

78118
const wordsToSentence = (words) => {
79119
// words is an array of strings
80120
// return a string that is all of the words concatenated together
81121
// spaces need to be between each word
82122
// example: ['Hello', 'world!'] -> 'Hello world!'
123+
return words.join(' ');
83124
};
84125

85126
const contains = (arr, item) => {
86127
// check to see if item is inside of arr
87128
// return true if it is, otherwise return false
129+
for (let i = 0; i < arr.length; i++) {
130+
if (arr[i] === item) {
131+
return true;
132+
}
133+
} return false;
88134
};
89135

90136
const addNumbers = (numbers) => {
91137
// numbers is an array of integers.
92138
// add all of the integers and return the value
139+
let sum = 0;
140+
for (let i = 0; i < numbers.length; i++) {
141+
sum += numbers[i];
142+
} return sum;
93143
};
94144

95145
const averageTestScore = (testScores) => {
96146
// testScores is an array. Iterate over testScores and compute the average.
97147
// return the average
148+
let sum = 0;
149+
for (let i = 0; i < testScores.length; i++) {
150+
sum += testScores[i];
151+
} return sum / testScores.length;
98152
};
99153

100154
const largestNumber = (numbers) => {
101155
// numbers is an array of integers
102156
// return the largest integer
157+
let largest = numbers[0];
158+
for (let i = 1; i < numbers.length; i++) {
159+
if (numbers[i] > largest) {
160+
largest = numbers[i];
161+
}
162+
} return largest;
103163
};
104164

105165
// Do not modify code below this line.

0 commit comments

Comments
 (0)