Skip to content

Commit fe460db

Browse files
committed
project 2 finished
1 parent 830e840 commit fe460db

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

src/project-1.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const areEqual = (x, y) => {
2323
// return true if x and y are the same
2424
// otherwise return false
2525
// code here
26-
return x === y;
26+
if (x === y) {
27+
return true;
28+
} return false;
2729
};
2830

2931
const lessThanNinety = (num) => {
@@ -152,7 +154,7 @@ const getTriangleArea = (base, height) => {
152154
const getCircleArea = (radius) => {
153155
// return the rounded area of the circle given the radius
154156
// code here
155-
return 3.14 * Math.pow(radius, 2);
157+
return Math.round(Math.PI * Math.pow(radius, 2));
156158
};
157159

158160
const getRectangularPrismVolume = (length, width, height) => {

src/project-2.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
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+
} else if (x < y) {
9+
return y;
10+
}
611
};
712

813
const greeting = (language) => {
@@ -11,15 +16,27 @@ const greeting = (language) => {
1116
// language: 'Spanish' -> 'Hola!'
1217
// language: 'Chinese' -> 'Ni Hao!'
1318
// if language is undefined return 'Hello!'
19+
switch (language) {
20+
case 'German':
21+
return 'Guten Tag!';
22+
case 'Chinese':
23+
return 'Ni Hao!';
24+
case 'Spanish':
25+
return 'Hola!';
26+
default:
27+
return 'Hello!';
28+
}
1429
};
1530

1631
const isTenOrFive = (num) => {
1732
// return true if num is 10 or 5
1833
// otherwise return false
34+
return num === 10 || num === 5;
1935
};
2036

2137
const isInRange = (num) => {
2238
// return true if num is less than 50 and greater than 20
39+
return num < 50 && num > 20;
2340
};
2441

2542
const isInteger = (num) => {
@@ -29,13 +46,25 @@ const isInteger = (num) => {
2946
// -10 -> true
3047
// otherwise return false
3148
// hint: you can solve this using Math.floor
49+
if (Math.floor(num) === num) {
50+
return true;
51+
}
52+
return false;
3253
};
3354

3455
const fizzBuzz = (num) => {
3556
// if num is divisible by 3 return 'fizz'
3657
// if num is divisible by 5 return 'buzz'
3758
// if num is divisible by 3 & 5 return 'fizzbuzz'
3859
// otherwise return num
60+
if (num % 5 === 0 && num % 3 === 0) {
61+
return 'fizzbuzz';
62+
} else if (num % 5 === 0) {
63+
return 'buzz';
64+
} else if (num % 3 === 0) {
65+
return 'fizz';
66+
}
67+
return num;
3968
};
4069

4170
const isPrime = (num) => {
@@ -44,62 +73,108 @@ const isPrime = (num) => {
4473
// hint: a prime number is only evenly divisible by itself and 1
4574
// hint2: you can solve this using a for loop
4675
// note: 0 and 1 are NOT considered prime numbers
76+
if (num < 0) {
77+
return false;
78+
}
79+
if (num === 1 || num === 0) {
80+
return false;
81+
}
82+
for (let i = 2; i < num; i++) {
83+
if (num % i === 0) {
84+
return false;
85+
}
86+
}
87+
return true;
4788
};
4889

4990
const returnFirst = (arr) => {
5091
// return the first item from the array
92+
return arr[0];
5193
};
5294

5395
const returnLast = (arr) => {
5496
// return the last item of the array
97+
return arr[arr.length - 1];
5598
};
5699

57100
const getArrayLength = (arr) => {
58101
// return the length of the array
102+
return arr.length;
59103
};
60104

61105
const incrementByOne = (arr) => {
62106
// arr is an array of integers
63107
// increase each integer by one
64108
// return the array
109+
for (let i = 0; i < arr.length; i++) {
110+
arr[i] += 1;
111+
}
112+
return arr;
65113
};
66114

67115
const addItemToArray = (arr, item) => {
68116
// add the item to the end of the array
69117
// return the array
118+
arr.push(item);
119+
return arr;
70120
};
71121

72122
const addItemToFront = (arr, item) => {
73123
// add the item to the front of the array
74124
// return the array
75125
// hint: use the array method .unshift
126+
arr.unshift(item);
127+
return arr;
76128
};
77129

78130
const wordsToSentence = (words) => {
79131
// words is an array of strings
80132
// return a string that is all of the words concatenated together
81133
// spaces need to be between each word
82134
// example: ['Hello', 'world!'] -> 'Hello world!'
135+
return words.join(' ');
83136
};
84137

85138
const contains = (arr, item) => {
86139
// check to see if item is inside of arr
87140
// return true if it is, otherwise return false
141+
for (let i = 0; i < arr.length; i++) {
142+
if (arr[i] === item) {
143+
return true;
144+
}
145+
}
146+
return false;
88147
};
89148

90149
const addNumbers = (numbers) => {
91150
// numbers is an array of integers.
92151
// add all of the integers and return the value
152+
let sum = 0;
153+
for (let i = 0; i < numbers.length; i++) {
154+
sum += numbers[i];
155+
}
156+
return sum;
93157
};
94158

95159
const averageTestScore = (testScores) => {
96160
// testScores is an array. Iterate over testScores and compute the average.
97161
// return the average
162+
let sum = 0;
163+
for (let i = 0; i < testScores.length; i++) {
164+
sum += testScores[i];
165+
}
166+
return sum / testScores.length;
98167
};
99168

100169
const largestNumber = (numbers) => {
101170
// numbers is an array of integers
102171
// return the largest integer
172+
let max = 0;
173+
for (let i = 0; i < numbers.length; i++) {
174+
if (max < numbers[i]) {
175+
max = numbers[i];
176+
}
177+
} return max;
103178
};
104179

105180
// Do not modify code below this line.

0 commit comments

Comments
 (0)