Created
October 12, 2022 08:08
-
-
Save buymed-hoangpham/d1040ebcdc2f2f7177b58b7eac802e40 to your computer and use it in GitHub Desktop.
exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1. Test your concepts of arrays by determining the location of an item in an array. | |
| const indexOf = (arr, item) => { | |
| // Your code here | |
| for (let i = 0; i < arr.length; i++) { | |
| if (arr[i] === item) return i; | |
| } | |
| return -1; | |
| }; | |
| // 2. Evaluate your concepts by populating an array. | |
| const sum = (arr) => { | |
| // Your code here | |
| return arr.reduce((acc, cur) => (acc += cur)); | |
| }; | |
| // 3. Evaluate yourself by returning a new array after removing all instances of a value from an array. | |
| // Returns a new array | |
| const remove = (arr, item) => { | |
| // Your code here | |
| return arr.filter((el) => el !== item); | |
| }; | |
| // 4. Evaluate yourself by adding an item to the end of an array | |
| const append = (arr, item) => { | |
| // Your code here | |
| arr.push(item); | |
| return arr; | |
| }; | |
| // 5. Evaluate yourself by removing the last item of an array | |
| const truncate = (arr) => { | |
| // Your code here | |
| arr.pop(); | |
| return arr; | |
| }; | |
| // 6. Test yourself by Adding an item at the beginning of an array. | |
| // Prepends to the original array | |
| const prepend = (arr, item) => { | |
| // Your code here | |
| arr.unshift(item); | |
| return arr; | |
| }; | |
| // 7. Test yourself by removing the first item of an array. | |
| // Modifies the original array | |
| const curtail = (arr) => { | |
| // Your code here | |
| arr.shift(); | |
| return arr; | |
| }; | |
| // 8. Test yourself by adding an item anywhere in an array. | |
| // Inserts an element in the original array | |
| const insert = (arr, item, index) => { | |
| // Your code here | |
| arr.splice(index, 0, item); | |
| return arr; | |
| }; | |
| // 9. Test yourself by counting the occurrences of an item in an array. | |
| const count = (arr, item) => { | |
| // Your code here | |
| return arr.reduce((acc, cur) => (cur === item ? acc++ : acc), 0); | |
| }; | |
| // 10. Test yourself by finding the duplicates in an array. | |
| const duplicates = (arr) => { | |
| // Your code here | |
| const count = new Set(); | |
| return arr.filter((num) => count.size === count.add(num).size); | |
| }; | |
| // 11. Test yourself by Squaring each number in an array. | |
| const square = (arr) => { | |
| // Your code here | |
| return arr.map((el) => Math.pow(el, 2)); | |
| }; | |
| // 12. Test yourself by finding all occurrences of an item in an array. | |
| const findAllOccurrences = (arr, target) => { | |
| let result = []; | |
| for (let i = 0; i < arr.length; i++) { | |
| if (arr[i] === target) result.push(i); | |
| } | |
| return result; | |
| }; | |
| // Exercise 2: Make Maximum Donuts | |
| const maxDonuts = (recipe, quantity) => { | |
| let rs = 0; | |
| for (let i of Object.keys(recipe)) { | |
| if (Math.floor(quantity[i] / recipe[i]) < 1) return 0; | |
| if (Math.floor(quantity[i] / recipe[i]) < rs || rs === 0) | |
| rs = Math.floor(quantity[i] / recipe[i]); | |
| } | |
| return rs; | |
| }; | |
| // Exercise 3: Calculate Score | |
| const records = [ | |
| { | |
| value: 24, | |
| gender: 'BOYS', | |
| }, | |
| { | |
| value: 42, | |
| gender: 'BOYS', | |
| }, | |
| { | |
| value: 85, | |
| gender: 'GIRLS', | |
| }, | |
| { | |
| value: 12, | |
| gender: 'GIRLS', | |
| }, | |
| { | |
| value: 10, | |
| gender: 'BOYS', | |
| }, | |
| ]; | |
| const sumOfValues = records.reduce( | |
| (acc, cur) => (cur.gender === 'BOYS' ? (acc += cur.value) : acc), | |
| 0 | |
| ); | |
| // Evaluate your string concepts by reducing the consecutive duplicate characters to the desired minimum. | |
| const reduceString = function (str, amount) { | |
| var re = new RegExp('(.)(?=\\1{' + amount + '})', 'g'); | |
| return str.replace(re, ''); | |
| }; | |
| // Test yourself by reversing a string. | |
| const reverseString = (str) => str.split('').reverse().join(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment