├── logo ├── .DS_Store ├── awesome_logo.jpg └── awesome_js_kata_logo.psd ├── CONTRIBUTING.md ├── filter └── README.md ├── map └── README.md ├── README.md └── reduce └── README.md /logo/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ouadie-lahdioui/awesome-javascript-katas/HEAD/logo/.DS_Store -------------------------------------------------------------------------------- /logo/awesome_logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ouadie-lahdioui/awesome-javascript-katas/HEAD/logo/awesome_logo.jpg -------------------------------------------------------------------------------- /logo/awesome_js_kata_logo.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ouadie-lahdioui/awesome-javascript-katas/HEAD/logo/awesome_js_kata_logo.psd -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | Please ensure your pull request adheres to the following guidelines: 4 | 5 | - Search previous suggestions before making a new one, as yours may be a duplicate. 6 | - Suggested kata should be tested. 7 | - Make an individual pull request for each suggestion. 8 | - Use the following format: `[kata name](link) - Description.` 9 | - New categories, or improvements to the existing categorization are welcome. 10 | - Keep descriptions short and simple, but descriptive. 11 | - End all descriptions with a full stop/period. 12 | - Check your spelling and grammar. 13 | 14 | Thank you for your suggestions ! -------------------------------------------------------------------------------- /filter/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Exercice 1: Filtrer les nombres pairs d’un tableau 3 | ``` 4 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8]; 5 | console.log(numbers.filter(value => value%2 === 0)) 6 | ``` 7 | 8 | ### Exercice 2: Garder uniquement les mots de plus de 5 lettres 9 | ``` 10 | const words = ["chat", "éléphant", "chien", "hippopotame"]; 11 | console.log(words.filter(value => value.length > 5)) 12 | ``` 13 | 14 | ### Exercice 3: Récupérer les utilisateurs majeurs d’un tableau d’objets 15 | ``` 16 | const users = [ 17 | { name: "Alice", age: 25 }, 18 | { name: "Bob", age: 17 }, 19 | { name: "Charlie", age: 19 } 20 | ]; 21 | console.log(users.filter((({age}) => age >= 18))); 22 | ``` 23 | ### Exercice 4: Supprimer les valeurs nulles ou indéfinies d’un tableau 24 | ``` 25 | const values = [10, null, 20, undefined, 30, "", 40]; 26 | console.log(values.filter(value => value)); -------------------------------------------------------------------------------- /map/README.md: -------------------------------------------------------------------------------- 1 | ### Exercice 1: Doubler les valeurs d’un tableau 2 | ``` 3 | const numbers = [1, 2, 3, 4, 5]; 4 | console.log(numbers.map(value => value*2)) 5 | ``` 6 | 7 | ### Exercice 2: Convertir un tableau de prix en TVA incluse (20%) 8 | ``` 9 | const prices = [10, 20, 30, 40]; 10 | console.log(prices.map(value => value + (value * 0.2))) 11 | ``` 12 | 13 | ### Exercice 3: Transformer un tableau de noms en majuscules 14 | ``` 15 | const names = ["alice", "bob", "charlie"]; 16 | console.log(names.map(value => value.toUpperCase())) 17 | ``` 18 | 19 | ### Exercice 4: Extraire les noms d’un tableau d’objets 20 | ``` 21 | const users = [ 22 | { name: "Alice", age: 25 }, 23 | { name: "Bob", age: 30 }, 24 | { name: "Charlie", age: 22 } 25 | ]; 26 | console.log(users.map(({name, age}) => name)) 27 | ``` 28 | 29 | ### Exercice 5: Créer un tableau de longueurs de mots 30 | ``` 31 | const words = ["apple", "banana", "cherry"]; 32 | console.log(words.map(value => value.length)) 33 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | Awesome Javascript Katas [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) 6 | ===================== 7 | 8 | A curated list of javascript katas to improve your coding skills. For more awesomeness, check out [awesome](https://github.com/sindresorhus/awesome). 9 | 10 | ## Katas 11 | 12 | - [Last](https://github.com/ouadie-lahdioui/last) - Javascript kata to find the last element of a list. 13 | - [Broken Greetings](https://github.com/ouadie-lahdioui/broken-greetings) - Javascript kata to correct a greet function. 14 | - [Unpacking Arguments](https://github.com/ouadie-lahdioui/unpacking-arguments) - Javascript kata to call a given function/lambda with a given arguments. 15 | - [For the sake of argument](https://github.com/ouadie-lahdioui/for-the-sake-of-argument) - Javascript kata to return true if all the function parameters are of the Number type. 16 | - [Duplicate Arguments](https://github.com/ouadie-lahdioui/duplicate-arguments) - Javascript kata to check for duplication. 17 | - [Calculating averages](https://github.com/ouadie-lahdioui/calculating-averages) - Javascript kata to calculate the average for an arbitrary number of arguments. 18 | - [Anonymous Returns](https://github.com/ouadie-lahdioui/anonymous-returns) - Javascript kata to make function called by itself bound to correct context. 19 | 20 | ## Functions 21 | - [.filter()](filter/README.md) - Javascript .filter() function. 22 | - [.map()](map/README.md) - Javascript .map() function. 23 | - [.reduce()](reduce/README.md) - Javascript .reduce() function. 24 | 25 | 26 | ## Contributing 27 | 28 | Your contributions are always welcome! Please read the [contribution guidelines](CONTRIBUTING.md) first. 29 | 30 | 31 | ## License 32 | 33 | [![CC0](http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/) 34 | 35 | To the extent possible under law, [Ouadie Lahdioui](https://twitter.com/lahdiouiouadie) has waived all copyright and related or neighboring rights to this work. -------------------------------------------------------------------------------- /reduce/README.md: -------------------------------------------------------------------------------- 1 | ### Excercice 1: find a sum of all numbers that are bigger or equal to 3 2 | 3 | ``` 4 | const arr = [1, 2, 3, 5, 6, 7, 4, 1]; 5 | 6 | function getSumBiggerThan(arr, value) { 7 | const reducer = (acc, value) => acc + value; 8 | return arr.filter(arrValue => arrValue >= value).reduce(reducer, 0); 9 | } 10 | 11 | console.log(getSumBiggerThen(arr, 3)); 12 | ``` 13 | 14 | ### Excercice 2: return a count of duplicate words in that string 15 | ``` 16 | const str = 'one two three two one one four five'; // 2 17 | 18 | function countDuplicate(str) { 19 | const strArray = str.split(" "); 20 | const reducer = (acc, value) => { 21 | acc[value] = (acc[value] || 0) + 1; 22 | return acc; 23 | } 24 | const strObject = strArray.reduce(reducer, {}); 25 | return Object.values(strObject).filter(value => value > 1).length; 26 | } 27 | console.log(countDuplicate(str)) 28 | ``` 29 | 30 | 31 | ### Excercice 3: Use reduce() to find the sum 32 | ``` 33 | const numbers = [1, 2, 3, 4, 5]; 34 | console.log(numbers.reduce((acc, value) => acc + value, 0)); 35 | ``` 36 | 37 | ### Excercice 4: Use reduce() to find the max value 38 | ``` 39 | const numbers = [10, 5, 20, 8, 15]; 40 | console.log(numbers.reduce( (acc, value) => { 41 | return value >= acc ? value : acc; 42 | },0)); 43 | ``` 44 | 45 | 46 | ### Exercice 5: Remove duplicates from an array using reduce(): 47 | 48 | ``` 49 | const numbers = [1, 2, 2, 3, 4, 4, 5]; 50 | 51 | const uniqueNumbers = numbers.reduce((acc, num) => { 52 | if (!acc.includes(num)) acc.push(num); 53 | return acc; 54 | }, []); 55 | 56 | console.log(uniqueNumbers); // [1, 2, 3, 4, 5] 57 | ``` 58 | 59 | ### Excercice 6: Transform an array of key-value pairs into an object using reduce() 60 | ``` 61 | const keyValuePairs = [['name', 'Alice'], ['age', 25], ['city', 'Paris']]; 62 | 63 | const obj = keyValuePairs.reduce((acc, [key, value]) => { 64 | acc[key] = value; 65 | return acc; 66 | }, {}); 67 | 68 | console.log(obj); // { name: 'Alice', age: 25, city: 'Paris' } 69 | 70 | // Since ES2019, Object.fromEntries() provides a built-in way to do this: 71 | const obj = Object.fromEntries(keyValuePairs); 72 | console.log(obj); // { name: 'Alice', age: 25, city: 'Paris' } 73 | ``` 74 | 75 | ### Excercice 7: Use reduce() to count occurrences 76 | ``` 77 | const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; 78 | 79 | function countOccurences(fruits) { 80 | const reducer = (acc, value) => { 81 | acc[value] = (acc[value] || 0) + 1 82 | return acc; 83 | }; 84 | 85 | return fruits.reduce(reducer, {}); 86 | } 87 | 88 | console.log(countOccurences(fruits)); 89 | ``` 90 | 91 | ### Excercice 7: Use reduce() to flatten the array 92 | ``` 93 | const nested = [[1, 2], [3, 4], [5, 6]]; 94 | 95 | function flatten(nested) { 96 | const reducer = (acc, value) => acc.concat(value) 97 | 98 | return nested.reduce(reducer, []); 99 | } 100 | 101 | console.log(flatten(nested)); 102 | ``` 103 | 104 | ### Excercice 8: Use reduce() to concatenate into a single string 105 | const words = ['Hello', 'world', 'this', 'is', 'JavaScript']; 106 | 107 | function concatenation(words) { 108 | return words.reduce((acc, value) => `${acc} ${value}`, "").trim(); 109 | } 110 | 111 | console.log(concatenation(words)); 112 | 113 | // Alternative using join() 114 | const sentence = words.join(' '); 115 | console.log(sentence); // "Hello world this is JavaScript" 116 | 117 | ### Exercice 9: Use reduce() to calculate total cart price 118 | const cart = [ 119 | { product: 'Laptop', price: 1000, quantity: 2 }, 120 | { product: 'Mouse', price: 50, quantity: 3 }, 121 | { product: 'Keyboard', price: 80, quantity: 1 } 122 | ]; 123 | 124 | 125 | function getTotalCart(cart) { 126 | return cart.reduce( (acc, { price, quantity }) => acc + (price * quantity), 0); 127 | }; 128 | 129 | console.log(getTotalCart(cart)); // 2 230 130 | 131 | 132 | ### Exercice 10: Use reduce() to count character occurrences 133 | const text1 = "hello world"; 134 | const text2 = "one two three two three three"; 135 | 136 | function getCharCount(text, spliter) { 137 | const reducer = (acc, value) => { 138 | if(value !== ' ') { 139 | acc[value] = (acc[value] || 0) + 1 140 | } 141 | return acc; 142 | }; 143 | 144 | return text.split(spliter).reduce(reducer, {}); 145 | }; 146 | 147 | console.log(getCharCount(text1, '')); // { h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1 } 148 | console.log(getCharCount(text2, ' ')); // { one: 1, two: 2, three: 3 } 149 | --------------------------------------------------------------------------------