|
| 1 | +/**** QUESTION 1: Printing Hashes ****/ |
| 2 | +console.log('*** QUESTION 1: Printing Hashes ***'); |
| 3 | +const printHashes = () => { |
| 4 | + let hash = ''; |
| 5 | + for (let i = 0; i < 7; i++) { |
| 6 | + hash += '#'; |
| 7 | + console.log(hash); |
| 8 | + } |
| 9 | +} |
| 10 | +printHashes(); |
| 11 | +/**** QUESTION 2: FizzBuz ****/ |
| 12 | +console.log('*** QUESTION 2: FizzBuz ***'); |
| 13 | +const fizzBuz = () => { |
| 14 | + for (let i = 1; i <= 100; i++) { |
| 15 | + if (i % 3 === 0 && i % 5 === 0) { |
| 16 | + console.log('FizBuzz', i); |
| 17 | + } else if (i % 3 !== 0 && i % 5 == 0) { |
| 18 | + console.log('Buz', i); |
| 19 | + } else if (i % 3 == 0) { |
| 20 | + console.log('Fizz', i); |
| 21 | + } else { |
| 22 | + console.log(i); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | +fizzBuz(); |
| 27 | +/**** QUESTION 3: Maximum *** |
| 28 | + * |
| 29 | + * |
| 30 | + */ |
| 31 | +console.log('*** QUESTION 3: Maximum ***'); |
| 32 | +const findMax = (a, b, c) => { |
| 33 | + let max; |
| 34 | + if (a > b && a > c) { |
| 35 | + max = a; |
| 36 | + } else if (b > a && b > c) { |
| 37 | + max = b; |
| 38 | + } else { |
| 39 | + max = c; |
| 40 | + } |
| 41 | + return max; |
| 42 | +} |
| 43 | +console.log(findMax(10, -9, 5)); |
| 44 | +console.log(findMax(-10, -9, -20)); |
| 45 | +console.log(findMax(-10, -9, 20)); |
| 46 | + |
| 47 | +/* |
| 48 | + *** QUESTION 4: Reverse Array *** |
| 49 | + */ |
| 50 | +console.log('*** QUESTION 4: Reverse Array ***'); |
| 51 | +const reverseArray = (arr) => { |
| 52 | + let newArr = []; |
| 53 | + let len = arr.length - 1; |
| 54 | + for (let i = 0; i <= len; i++) { |
| 55 | + newArr[i] = arr[len - i]; |
| 56 | + } |
| 57 | + return newArr; |
| 58 | +} |
| 59 | +console.log(reverseArray([1, 2, 3, 4, 5])); |
| 60 | +console.log(reverseArray(['A', 'B', 'C'])); |
| 61 | + |
| 62 | +/* |
| 63 | + *** QUESTION 5: Modify Array*** |
| 64 | + */ |
| 65 | +console.log('*** QUESTION 5: Modify Array***'); |
| 66 | +const modifyArray = (arr) => { |
| 67 | + let modifiedArr = []; |
| 68 | + if (arr.length < 5) { |
| 69 | + return 'Not Found'; |
| 70 | + } |
| 71 | + for (let i = 0; i < arr.length; i++) |
| 72 | + i === 4 |
| 73 | + ? (modifiedArr[i] = arr[i].toUpperCase()) |
| 74 | + : (modifiedArr[i] = arr[i]); |
| 75 | + |
| 76 | + return modifiedArr; |
| 77 | +} |
| 78 | +console.log( |
| 79 | + modifyArray(['Avocado', 'Tomato', 'Potato', 'Mango', 'Lemon', 'Carrot']) |
| 80 | +); |
| 81 | + |
| 82 | +/* |
| 83 | + *** QUESTION 6 : Seven unique random numbers in an array*** |
| 84 | + */ |
| 85 | +console.log('*** QUESTION 6 : Seven unique random numbers in an array***'); |
| 86 | +// solution 1 |
| 87 | +function sevenRandomNumbers() { |
| 88 | + const randNumbers = []; |
| 89 | + while (randNumbers.length < 7) { |
| 90 | + const randNum = Math.floor(Math.random() * 9) + 1; |
| 91 | + if (randNumbers.indexOf(randNum) === -1) { |
| 92 | + randNumbers.push(randNum); |
| 93 | + } |
| 94 | + } |
| 95 | + return randNumbers; |
| 96 | +} |
| 97 | + |
| 98 | +console.log(sevenRandomNumbers()); |
| 99 | +// solution 2 |
| 100 | +function sevenRandomNumbers() { |
| 101 | + const randNumbers = []; |
| 102 | + let i = 0; |
| 103 | + let randNum; |
| 104 | + let len = randNumbers.length; |
| 105 | + while (i < 7) { |
| 106 | + randNum = Math.floor(Math.random() * 10 + 1); |
| 107 | + if (i == 0) { |
| 108 | + randNumbers[i] = randNum; |
| 109 | + } else { |
| 110 | + if (randNumbers.indexOf(randNum) == -1) { |
| 111 | + randNumbers[i] = randNum; |
| 112 | + } else { |
| 113 | + i--; |
| 114 | + } |
| 115 | + } |
| 116 | + i++; |
| 117 | + } |
| 118 | + |
| 119 | + return randNumbers; |
| 120 | +} |
| 121 | +console.log(sevenRandomNumbers()); |
| 122 | +function sevenRandomNumber() { |
| 123 | + const randNumbers = []; |
| 124 | + while (array.length < 7) { |
| 125 | + const random = Math.floor(Math.random() * 9); |
| 126 | + if (randNumbers.indexOf(random) === -1) { |
| 127 | + randNumbers.push(random); |
| 128 | + } |
| 129 | + } |
| 130 | + console.log(randNumbers); |
| 131 | + return randNumbers; |
| 132 | +} |
| 133 | + |
| 134 | +sevenRandomNumber(); |
| 135 | + |
| 136 | +/* |
| 137 | + *** QUESTION 7: Sum *** |
| 138 | + */ |
| 139 | +console.log('*** QUESTION 7: Sum of any number of arguments***'); |
| 140 | +const sumOfArgs = (...args) => { |
| 141 | + let total = 0; |
| 142 | + args.forEach(arg => (total += arg)); |
| 143 | + return total; |
| 144 | +}; |
| 145 | +console.log(sumOfArgs(1, 2, 3)); |
| 146 | +console.log(sumOfArgs(1, 2, 3, 4)); |
| 147 | + |
| 148 | +function sum() { |
| 149 | + let total = 0; |
| 150 | + Array.from(arguments).forEach(arg => (total += arg)); |
| 151 | + return total; |
| 152 | +} |
| 153 | + |
| 154 | +console.log(sum(1, 2, 3)); |
| 155 | +console.log(sum(1, 2, 3, 4)); |
| 156 | +/* |
| 157 | + *** QUESTION 8: Replace the middle item with three items*** |
| 158 | + */ |
| 159 | +console.log('*** QUESTION 8: Replace the middle item with three items***'); |
| 160 | +const removeMiddleItem = (arr, itemOne, itemTwo, itemThree) => { |
| 161 | + let arrayLen = arr.length; |
| 162 | + let middleIndex; |
| 163 | + if (arrayLen % 2 === 0) { |
| 164 | + middleIndex = arrayLen / 2 - 1; |
| 165 | + arr.splice(middleIndex, 2, itemOne, itemTwo, itemThree); |
| 166 | + } else { |
| 167 | + middleIndex = (arrayLen + 1) / 2 - 1; |
| 168 | + arr.splice(middleIndex, 1, itemOne, itemTwo, itemThree); |
| 169 | + } |
| 170 | + return arr; |
| 171 | +} |
| 172 | +console.log(removeMiddleItem([1, 2, 3], 'item 1', 'item2', 'item3')); |
| 173 | +console.log(removeMiddleItem([1, 2, 3, 4], 'item 1', 'item2', 'item3')); |
| 174 | +console.log(removeMiddleItem([1, 2, 3], 4, 5, 6)); |
| 175 | +/* |
| 176 | + *** QUESTION 9: Extract numbers from text *** |
| 177 | + */ |
| 178 | +console.log('*** QUESTION 9: Extract numbers from a text ***'); |
| 179 | +const calculateAnnualIncome = () => { |
| 180 | + const pattern = /[0-9]+/g; |
| 181 | + const incomes = 'He earns 5000 euro from salary per month, 10000 euro annual bonus, 15000 euro online courses per month.'.match( |
| 182 | + pattern |
| 183 | + ); |
| 184 | + let sum = 0; |
| 185 | + incomes.forEach((income, i) => { |
| 186 | + if (i == 0 || i == 2) { |
| 187 | + sum += parseFloat(income) * 12; |
| 188 | + } else { |
| 189 | + sum += parseFloat(income); |
| 190 | + } |
| 191 | + }); |
| 192 | + return sum; |
| 193 | +} |
| 194 | +console.log(calculateAnnualIncome()); |
| 195 | +/* |
| 196 | + *** QUESTION 10: Check if a sub string is an end of a text *** |
| 197 | + */ |
| 198 | +console.log('*** QUESTION 10: Check if a sub string is an end of a text ***'); |
| 199 | +const checkEndOfString = (mainString, subString) => { |
| 200 | + return mainString.endsWith(subString); |
| 201 | +} |
| 202 | +console.log(checkEndOfString('integrity', 'ity')); |
| 203 | +console.log(checkEndOfString('integration', 'tio')); |
0 commit comments