|
1 | | -// Do not change any of the function names |
2 | | - |
3 | 1 | const getBiggest = (x, y) => { |
4 | | - // x and y are integers. Return the larger integer |
5 | | - // if they are the same return either one |
| 2 | + if (x > y) { |
| 3 | + return x; |
| 4 | + } else if (x === y) { |
| 5 | + return y; |
| 6 | + } |
| 7 | + return y; |
6 | 8 | }; |
7 | 9 |
|
8 | 10 | const greeting = (language) => { |
9 | | - // return a greeting for three different languages: |
10 | | - // language: 'German' -> 'Guten Tag!' |
11 | | - // language: 'Spanish' -> 'Hola!' |
12 | | - // language: 'Chinese' -> 'Ni Hao!' |
13 | | - // if language is undefined return 'Hello!' |
| 11 | + switch (language) { |
| 12 | + case 'German': |
| 13 | + return 'Guten Tag!'; |
| 14 | + case 'Spanish': |
| 15 | + return 'Hola!'; |
| 16 | + case 'Chinese': |
| 17 | + return 'Ni Hao!'; |
| 18 | + default: |
| 19 | + return 'Hello!'; |
| 20 | + } |
14 | 21 | }; |
15 | 22 |
|
16 | 23 | const isTenOrFive = (num) => { |
17 | | - // return true if num is 10 or 5 |
18 | | - // otherwise return false |
| 24 | + if (num === 10 || num === 5) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + return false; |
19 | 28 | }; |
20 | 29 |
|
21 | 30 | const isInRange = (num) => { |
22 | | - // return true if num is less than 50 and greater than 20 |
| 31 | + if (num < 50 && num > 20) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + return false; |
23 | 35 | }; |
24 | 36 |
|
25 | 37 | const isInteger = (num) => { |
26 | | - // return true if num is an integer |
27 | | - // 0.8 -> false |
28 | | - // 1 -> true |
29 | | - // -10 -> true |
30 | | - // otherwise return false |
31 | | - // hint: you can solve this using Math.floor |
| 38 | + if (Math.floor(num) === num) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + return false; |
32 | 42 | }; |
33 | 43 |
|
34 | 44 | const fizzBuzz = (num) => { |
35 | | - // if num is divisible by 3 return 'fizz' |
36 | | - // if num is divisible by 5 return 'buzz' |
37 | | - // if num is divisible by 3 & 5 return 'fizzbuzz' |
38 | | - // otherwise return num |
| 45 | + if (num % 5 === 0 && num % 3 === 0) { |
| 46 | + return 'fizzbuzz'; |
| 47 | + } else if (num % 5 === 0) { |
| 48 | + return 'buzz'; |
| 49 | + } else if (num % 3 === 0) { |
| 50 | + return 'fizz'; |
| 51 | + } |
| 52 | + return num; |
39 | 53 | }; |
40 | 54 |
|
41 | 55 | const isPrime = (num) => { |
42 | | - // return true if num is prime. |
43 | | - // otherwise return false |
44 | | - // hint: a prime number is only evenly divisible by itself and 1 |
45 | | - // hint2: you can solve this using a for loop |
46 | | - // note: 0 and 1 are NOT considered prime numbers |
| 56 | + if (num < 0) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + if (num === 1 || num === 0) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + for (let i = 2; i < num; i++) { |
| 63 | + if (num % i === 0) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + return true; |
47 | 68 | }; |
48 | 69 |
|
49 | 70 | const returnFirst = (arr) => { |
50 | | - // return the first item from the array |
| 71 | + return arr[0]; |
51 | 72 | }; |
52 | 73 |
|
53 | 74 | const returnLast = (arr) => { |
54 | | - // return the last item of the array |
| 75 | + return arr[arr.length - 1]; |
55 | 76 | }; |
56 | 77 |
|
57 | 78 | const getArrayLength = (arr) => { |
58 | | - // return the length of the array |
| 79 | + return arr.length; |
59 | 80 | }; |
60 | 81 |
|
61 | 82 | const incrementByOne = (arr) => { |
62 | | - // arr is an array of integers |
63 | | - // increase each integer by one |
64 | | - // return the array |
| 83 | + for (let i = 0; i < arr.length; i++) { |
| 84 | + arr[i]++; |
| 85 | + } |
| 86 | + return arr; |
65 | 87 | }; |
66 | 88 |
|
67 | 89 | const addItemToArray = (arr, item) => { |
68 | | - // add the item to the end of the array |
69 | | - // return the array |
| 90 | + arr.push(item); |
| 91 | + return arr; |
70 | 92 | }; |
71 | 93 |
|
72 | 94 | const addItemToFront = (arr, item) => { |
73 | | - // add the item to the front of the array |
74 | | - // return the array |
75 | | - // hint: use the array method .unshift |
| 95 | + arr.unshift(item); |
| 96 | + return arr; |
76 | 97 | }; |
77 | 98 |
|
78 | 99 | const wordsToSentence = (words) => { |
79 | | - // words is an array of strings |
80 | | - // return a string that is all of the words concatenated together |
81 | | - // spaces need to be between each word |
82 | | - // example: ['Hello', 'world!'] -> 'Hello world!' |
| 100 | + let newSentence = ''; |
| 101 | + for (let i = 0; i < words.length; i++) { |
| 102 | + if (i === 0) { |
| 103 | + newSentence += `${words[i]}`; |
| 104 | + } else { |
| 105 | + newSentence += ` ${words[i]}`; |
| 106 | + } |
| 107 | + } |
| 108 | + return newSentence; |
83 | 109 | }; |
84 | 110 |
|
85 | 111 | const contains = (arr, item) => { |
86 | | - // check to see if item is inside of arr |
87 | | - // return true if it is, otherwise return false |
| 112 | + let itemCounter = 0; |
| 113 | + for (let i = 0; i < arr.length; i++) { |
| 114 | + if (arr[i] === item) { |
| 115 | + itemCounter++; |
| 116 | + } |
| 117 | + } |
| 118 | + if (itemCounter > 0) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + return false; |
88 | 122 | }; |
89 | 123 |
|
90 | 124 | const addNumbers = (numbers) => { |
91 | | - // numbers is an array of integers. |
92 | | - // add all of the integers and return the value |
| 125 | + let sumOfNumbers = 0; |
| 126 | + for (let i = 0; i < numbers.length; i++) { |
| 127 | + sumOfNumbers += numbers[i]; |
| 128 | + } |
| 129 | + return sumOfNumbers; |
93 | 130 | }; |
94 | 131 |
|
95 | 132 | const averageTestScore = (testScores) => { |
96 | | - // testScores is an array. Iterate over testScores and compute the average. |
97 | | - // return the average |
| 133 | + let totalSumScores = 0; |
| 134 | + let numberOfScore = 0; |
| 135 | + for (let i = 0; i < testScores.length; i++) { |
| 136 | + totalSumScores += testScores[i]; |
| 137 | + numberOfScore++; |
| 138 | + } |
| 139 | + return totalSumScores / numberOfScore; |
98 | 140 | }; |
99 | 141 |
|
100 | 142 | const largestNumber = (numbers) => { |
101 | | - // numbers is an array of integers |
102 | | - // return the largest integer |
| 143 | + let biggestInteger = 0; |
| 144 | + for (let i = 0; i < numbers.length; i++) { |
| 145 | + if (numbers[i] > biggestInteger) { |
| 146 | + biggestInteger = numbers[i]; |
| 147 | + } |
| 148 | + } |
| 149 | + return biggestInteger; |
103 | 150 | }; |
104 | 151 |
|
105 | | -// Do not modify code below this line. |
106 | | -// -------------------------------- |
107 | | - |
108 | 152 | module.exports = { |
109 | 153 | getBiggest, |
110 | 154 | greeting, |
|
0 commit comments