From d3a8b566462d4d3dcca5ab62a1b381ffda5e033d Mon Sep 17 00:00:00 2001 From: Clement Denis Date: Wed, 5 May 2021 23:26:56 +0100 Subject: [PATCH] discovery-js: add more exercises --- js/tests/about-time.json | 18 ++++++++++ js/tests/give-me-the-code.json | 14 ++++++++ js/tests/half-a-million.json | 8 ++--- js/tests/jaden-case.json | 18 ++++++++++ js/tests/names-plz.json | 14 ++++++++ js/tests/no-adults-wanted.json | 14 ++++++++ js/tests/speed-run.json | 18 ++++++++++ js/tests/squared.json | 10 ++++++ js/tests/two-can-play-that-game.json | 4 +-- subjects/3-directions/README.md | 6 ++++ subjects/a-winning-argument/README.md | 2 +- subjects/about-time/README.md | 38 +++++++++++++++++++++ subjects/access/README.md | 11 +++--- subjects/give-me-the-code/README.md | 21 ++++++++++++ subjects/half-a-million/README.md | 15 +++++++-- subjects/jaden-case/README.md | 17 ++++++++++ subjects/keep/README.md | 8 ++++- subjects/level-up/README.md | 4 +-- subjects/names-plz/README.md | 43 ++++++++++++++++++++++++ subjects/no-adults-wanted/README.md | 44 ++++++++++++++++++++++++ subjects/recursion/README.md | 3 ++ subjects/speed-run/README.md | 34 +++++++++++++++++++ subjects/squared/README.md | 48 +++++++++++++++++++++++++++ subjects/swapy/README.md | 4 +-- 24 files changed, 395 insertions(+), 21 deletions(-) create mode 100644 js/tests/about-time.json create mode 100644 js/tests/give-me-the-code.json create mode 100644 js/tests/jaden-case.json create mode 100644 js/tests/names-plz.json create mode 100644 js/tests/no-adults-wanted.json create mode 100644 js/tests/speed-run.json create mode 100644 js/tests/squared.json create mode 100644 subjects/about-time/README.md create mode 100644 subjects/give-me-the-code/README.md create mode 100644 subjects/jaden-case/README.md create mode 100644 subjects/names-plz/README.md create mode 100644 subjects/no-adults-wanted/README.md create mode 100644 subjects/recursion/README.md create mode 100644 subjects/speed-run/README.md create mode 100644 subjects/squared/README.md diff --git a/js/tests/about-time.json b/js/tests/about-time.json new file mode 100644 index 000000000..15e688445 --- /dev/null +++ b/js/tests/about-time.json @@ -0,0 +1,18 @@ +[ + { + "description": "Test TOP 2", + "code": "equal(\n ultimateScoreboard(\n [\n { duration: 122, name: 'Muntaser' },\n { duration: 235, name: 'Noura' },\n { duration: 927, name: 'Maitha' },\n ],\n 2,\n ),\n ['#01 - 02:02, Muntaser', '#02 - 03:55, Noura'],\n)" + }, + { + "description": "Test TOP 5", + "code": "equal(\n ultimateScoreboard(\n [\n { duration: 60, name: 'Wasan' },\n { duration: 237, name: 'Ibrahim' },\n { duration: 302, name: 'Alanoud' },\n { duration: 411, name: 'Hessa' },\n { duration: 499, name: 'Mariam' },\n { duration: 514, name: 'Meriem' },\n { duration: 524, name: 'Sarah' },\n { duration: 567, name: 'Mehnaz' },\n { duration: 891, name: 'maitha' },\n { duration: 967, name: 'Shadha' },\n { duration: 982, name: 'Hala' },\n { duration: 1124, name: 'Lina' },\n { duration: 1137, name: 'Alanoud' },\n { duration: 1138, name: 'Asmaa' },\n { duration: 1170, name: 'Abdulkarim' },\n ],\n 5,\n ),\n [\n '#01 - 01:00, Wasan',\n '#02 - 03:57, Ibrahim',\n '#03 - 05:02, Alanoud',\n '#04 - 06:51, Hessa',\n '#05 - 08:19, Mariam',\n ],\n)" + }, + { + "description": "Test top 10", + "code": "equal(\n ultimateScoreboard(\n [\n { duration: 101, name: 'Clement' },\n { duration: 120, name: 'Yahya' },\n { duration: 158, name: 'Rahimul' },\n { duration: 354, name: 'Muntaser' },\n { duration: 374, name: 'Marie' },\n { duration: 427, name: 'Jawaher' },\n { duration: 621, name: 'Hamda' },\n { duration: 841, name: 'Haya' },\n { duration: 860, name: 'Aaesha' },\n { duration: 1012, name: 'Karim' },\n { duration: 1041, name: 'Noor' },\n { duration: 1071, name: 'AM' },\n { duration: 1112, name: 'Mahraalk' },\n { duration: 1124, name: 'Ali' },\n { duration: 3797, name: 'Fady' },\n ],\n 10,\n ),\n [\n '#01 - 01:41, Clement',\n '#02 - 02:00, Yahya',\n '#03 - 02:38, Rahimul',\n '#04 - 05:54, Muntaser',\n '#05 - 06:14, Marie',\n '#06 - 07:07, Jawaher',\n '#07 - 10:21, Hamda',\n '#08 - 14:01, Haya',\n '#09 - 14:20, Aaesha',\n '#10 - 16:52, Karim',\n ],\n)" + }, + { + "description": "Test TOP 20", + "code": "equal(\n ultimateScoreboard(\n [\n { duration: 17, name: 'Masood' },\n { duration: 48, name: 'Mehnaz' },\n { duration: 95, name: 'Hessa' },\n { duration: 96, name: 'Fareaa' },\n { duration: 97, name: 'Mariam' },\n { duration: 133, name: 'shamma' },\n { duration: 143, name: 'Laila' },\n { duration: 144, name: 'Reem' },\n { duration: 222, name: 'aisha' },\n { duration: 242, name: 'Maitha' },\n { duration: 249, name: 'noura' },\n { duration: 427, name: 'Hello' },\n { duration: 452, name: 'Zahra' },\n { duration: 470, name: 'Aisha' },\n { duration: 594, name: 'Wasan' },\n { duration: 703, name: 'shaikha' },\n { duration: 716, name: 'Maitha' },\n { duration: 762, name: 'mariam' },\n { duration: 770, name: 'Tanzeel' },\n { duration: 796, name: 'Mohammed' },\n { duration: 803, name: 'Mibin' },\n { duration: 885, name: 'salma' },\n { duration: 899, name: 'Mai' },\n { duration: 905, name: 'khadija' },\n { duration: 1032, name: 'Fatima' },\n { duration: 1093, name: 'Salama' },\n { duration: 1194, name: 'Samira' },\n ],\n 20,\n ),\n [\n '#01 - 00:17, Masood',\n '#02 - 00:48, Mehnaz',\n '#03 - 01:35, Hessa',\n '#04 - 01:36, Fareaa',\n '#05 - 01:37, Mariam',\n '#06 - 02:13, shamma',\n '#07 - 02:23, Laila',\n '#08 - 02:24, Reem',\n '#09 - 03:42, aisha',\n '#10 - 04:02, Maitha',\n '#11 - 04:09, noura',\n '#12 - 07:07, Hello',\n '#13 - 07:32, Zahra',\n '#14 - 07:50, Aisha',\n '#15 - 09:54, Wasan',\n '#16 - 11:43, shaikha',\n '#17 - 11:56, Maitha',\n '#18 - 12:42, mariam',\n '#19 - 12:50, Tanzeel',\n '#20 - 13:16, Mohammed',\n ],\n)" + } +] \ No newline at end of file diff --git a/js/tests/give-me-the-code.json b/js/tests/give-me-the-code.json new file mode 100644 index 000000000..2e5558c06 --- /dev/null +++ b/js/tests/give-me-the-code.json @@ -0,0 +1,14 @@ +[ + { + "description": "Test with the example data", + "code": "equal(\n jsFileOnly([\n 'essay.docx',\n 'index.html',\n 'info.txt',\n 'lib.js',\n 'README.md',\n 'script.js',\n ]),\n ['lib.js', 'script.js'],\n)" + }, + { + "description": "Test with more data", + "code": "equal(\n jsFileOnly([\n '2021-05-03 10-20-34.mkv',\n 'app.js',\n 'app.min.js',\n 'com.debug',\n 'Component.jsx',\n 'config.yml',\n 'package.json',\n 'Pictures',\n 'service.log',\n ]),\n ['app.js', 'app.min.js'],\n)" + }, + { + "description": "Test with a larger dataset", + "code": "equal(\n jsFileOnly([\n 'LICENSE',\n 'README.md',\n 'api',\n 'api/auth.js',\n 'api/db.js',\n 'api/defs.js',\n 'api/router.js',\n 'api/server.js',\n 'app.jsx',\n 'component',\n 'component/elements.jsx',\n 'component/form.jsx',\n 'component/header.jsx',\n 'component/icons.jsx',\n 'data',\n 'data/ascii.js',\n 'data/discord.js',\n 'dev',\n 'dev/build.js',\n 'dev/ci.js',\n 'dev/deploy.js',\n 'dev/janus-discord.js',\n 'dev/janus-kv.js',\n 'dev/janus.js',\n 'dev/mocks.js',\n 'dev/request-runner.js',\n 'dev/server.js',\n 'dev/test-runner.js',\n 'dev/test_api_auth.js',\n 'dev/test_lib_eve.js',\n 'dev/test_lib_router.js',\n 'dev/utils.js',\n 'lib',\n 'lib/auth.js',\n 'lib/dom.js',\n 'lib/env.js',\n 'lib/eve.js',\n 'lib/hooks.js',\n 'lib/preact-shim-dev.js',\n 'lib/preact-shim.js',\n 'lib/router.js',\n 'lib/state.js',\n 'package-lock.json',\n 'package.json',\n 'page',\n 'page/home.jsx',\n 'page/profile.jsx',\n 'script',\n 'script/build.sh',\n 'script/deploy.sh',\n 'script/start.sh',\n 'script/test.sh',\n 'template',\n 'template/index.html',\n 'wrangler.toml',\n ]),\n [\n 'api/auth.js',\n 'api/db.js',\n 'api/defs.js',\n 'api/router.js',\n 'api/server.js',\n 'data/ascii.js',\n 'data/discord.js',\n 'dev/build.js',\n 'dev/ci.js',\n 'dev/deploy.js',\n 'dev/janus-discord.js',\n 'dev/janus-kv.js',\n 'dev/janus.js',\n 'dev/mocks.js',\n 'dev/request-runner.js',\n 'dev/server.js',\n 'dev/test-runner.js',\n 'dev/test_api_auth.js',\n 'dev/test_lib_eve.js',\n 'dev/test_lib_router.js',\n 'dev/utils.js',\n 'lib/auth.js',\n 'lib/dom.js',\n 'lib/env.js',\n 'lib/eve.js',\n 'lib/hooks.js',\n 'lib/preact-shim-dev.js',\n 'lib/preact-shim.js',\n 'lib/router.js',\n 'lib/state.js',\n ],\n)" + } +] \ No newline at end of file diff --git a/js/tests/half-a-million.json b/js/tests/half-a-million.json index 43b58c4c0..b08ce09f4 100644 --- a/js/tests/half-a-million.json +++ b/js/tests/half-a-million.json @@ -1,14 +1,14 @@ [ { "description": "You must retrieve '1/2 MM $' from the vault using the secret", - "code": "let secret = '53CR37-k0D3'\nlet vault = { '53CR37-k0D3': '1/2 MM $' }\n// Your code\nequal(loot, '1/2 MM $')" + "code": "let secret = '53CR37-k0D3'\nconst vault = { '53CR37-k0D3': '1/2 MM $' }\n\n// Your code\n\nequal(loot, '1/2 MM $')" }, { "description": "You must retrieve 500000 from the vault using the secret", - "code": "let secret = '53(.-3T / arg.join(' ')),\n [\n 'Batman and Robin !',\n 'Pinky and The Brain !',\n 'Bonnie and Clyde !',\n 'Mr. and Mrs.Smith !',\n ],\n)" + "code": "const args = saveArguments(console, 'log')\n\n// Your code\n\nduos('Batman', 'Robin')\nduos('Pinky', 'The Brain')\nduos('Bonnie', 'Clyde')\nduos('Mr.', 'Mrs.Smith')\n\nequal(\n args.map((arg) => arg.join(' ')),\n [\n 'Batman and Robin !',\n 'Pinky and The Brain !',\n 'Bonnie and Clyde !',\n 'Mr. and Mrs.Smith !',\n ],\n)" }, { "description": "duosWork is defined and is a function", @@ -21,6 +21,6 @@ }, { "description": "duosWork logs the expected result", - "code": "const args = saveArguments(console, 'log')\n\n// Your code\n\nduosWork('Batman', 'Robin', 'protect Gotham')\nduosWork('Pinky', 'The Brain', 'want to conquer the world')\nduosWork('Bonnie', 'Clyde', 'escape the Police')\nduosWork('Mr.', 'Mrs.Smith', 'are the greatest spy couple')\n\nequal(\n args.map(arg => arg.join(' ')),\n [\n 'Batman and Robin protect Gotham !',\n 'Pinky and The Brain want to conquer the world !',\n 'Bonnie and Clyde escape the Police !',\n 'Mr. and Mrs.Smith are the greatest spy couple !',\n ],\n)" + "code": "const args = saveArguments(console, 'log')\n\n// Your code\n\nduosWork('Batman', 'Robin', 'protect Gotham')\nduosWork('Pinky', 'The Brain', 'want to conquer the world')\nduosWork('Bonnie', 'Clyde', 'escape the Police')\nduosWork('Mr.', 'Mrs.Smith', 'are the greatest spy couple')\n\nequal(\n args.map((arg) => arg.join(' ')),\n [\n 'Batman and Robin protect Gotham !',\n 'Pinky and The Brain want to conquer the world !',\n 'Bonnie and Clyde escape the Police !',\n 'Mr. and Mrs.Smith are the greatest spy couple !',\n ],\n)" } ] \ No newline at end of file diff --git a/subjects/3-directions/README.md b/subjects/3-directions/README.md index 983d950e8..4c130e7c9 100644 --- a/subjects/3-directions/README.md +++ b/subjects/3-directions/README.md @@ -43,6 +43,12 @@ access them and assign their values to variables: - a variable `kiss` of an array of 2 elements, the last and the first element of the `list`, in that order. +**Example:** if `list` is `[1,2,3]` + +- `first` would be `1` +- `last` would be `3` +- `kiss` would be `[3, 1]` + > 🧑‍🎤 ............ Oh, ........... 🧑‍🎤 > 🎶 .. I wanna be last, yeah ... 🎶 > 🎵 . Baby let me be your last . 🎵 diff --git a/subjects/a-winning-argument/README.md b/subjects/a-winning-argument/README.md index 9bc191ebc..e872cc81a 100644 --- a/subjects/a-winning-argument/README.md +++ b/subjects/a-winning-argument/README.md @@ -27,7 +27,7 @@ We are now going to adapt `myFirstFuntion` so that it takes one argument : `arg1`. ```js -let myFirstFunction = arg1 => { +let myFirstFunction = (arg1) => { //<-arg1 is inputed in between the parens console.log(arg1) // arg1 can be use inside the scope of the function // ↖ arg1 is "transfered" to be the arg of console.log() diff --git a/subjects/about-time/README.md b/subjects/about-time/README.md new file mode 100644 index 000000000..4aa98929d --- /dev/null +++ b/subjects/about-time/README.md @@ -0,0 +1,38 @@ +## About Time + +### Instructions + +Create the function `ultimateScoreboard` that takes an array of scores, and a +`count` and produce the **ULTIMATE** score board: + +- Only show the TOP `count` peoples +- Format durations in `minutes:seconds` + + > ex: `123` -> `02:03` (123 seconds = 2 minutes and 3 seconds) + +- Format each scores with first the position in the scoreboard from `1`, then + the formated `duration` described previously and finally the name + + > ex: `[{ name: 'Wasan', duration: 93 }]` -> `['#01 - 01:33, Wasan']` \ + > note that numbers lower than 10 must be padded with a leading 0 + +**Example**: + +```js +let scores = [ + { name: 'Wasan', duration: 93 }, + { name: 'Muntaser', duration: 122 }, + { name: 'Noura', duration: 235 }, + { name: 'Maitha', duration: 927 }, +] + +let scoreboard = ultimateScoreboard(scores, 3) + +console.log(scoreboard) /* [ + '#01 - 01:33, Wasan', + '#02 - 02:02, Muntaser', + '#03 - 03:55, Noura', +] +``` + +> This time, no more instructions, find the steps yourselfs, good luck ! diff --git a/subjects/access/README.md b/subjects/access/README.md index acb40f191..5cc51c3d6 100644 --- a/subjects/access/README.md +++ b/subjects/access/README.md @@ -7,10 +7,10 @@ Now that we know how to define objects, let's start to use them Let's start with getting values from them. Well, turns out you already have been doing it a lot, remember `console.log` ? -The `.` here is *accessing* the property `log` from the `console` object. +The `.` here is _accessing_ the property `log` from the `console` object. -Taking the previous example `transaction` object, we can access properties -from it using `.` same way we got the `log` function: +Taking the previous example `transaction` object, we can access properties from +it using `.` same way we got the `log` function: ```js let transaction = { @@ -23,9 +23,8 @@ console.log(transaction) // Will log the whole transaction console.log(transaction.amount) // will only log the amount of the transaction ``` -Accessing a property with `.` only give you it's value, and -because it is a value you can use it like any other values: - +Accessing a property with `.` only give you it's value, and because it is a +value you can use it like any other values: ```js let taxes = 1.2 // let's define 20% taxes diff --git a/subjects/give-me-the-code/README.md b/subjects/give-me-the-code/README.md new file mode 100644 index 000000000..3e675f6b1 --- /dev/null +++ b/subjects/give-me-the-code/README.md @@ -0,0 +1,21 @@ +## Give me the code + +### Instructions + +Create a `jsFileOnly` function that takes an array of filenames (strings) and +only keeps those that are ending with `.js` + +**Example:** + +```js +const result = jsFileOnly([ + 'essay.docx', + 'index.html', + 'info.txt', + 'lib.js', + 'README.md', + 'script.js', +]) + +console.log(result) // ['lib.js', 'script.js'] +``` diff --git a/subjects/half-a-million/README.md b/subjects/half-a-million/README.md index 2930bbb6d..7edd80847 100644 --- a/subjects/half-a-million/README.md +++ b/subjects/half-a-million/README.md @@ -44,10 +44,19 @@ variables: - a `vault` object - a `secret` string -You must use the `secret` value as a key to access the matching value from the -`vault` and assign it to a variable `loot`. +Your goal is to steal the `secret` matching value from the `vault`. -You must then replace the value from the vault by a message of your choice. +Declare a variable `loot` and assign it's value using the variable `secret` +value as a key to access the matching value from the `vault`. + +You must then replace the value from the vault's secret by a message of your +choice. + +**Example**: if `vault` is `{ '53CR37-k0D3': '1/2 MM $' }` and `secret` is +`'53CR37-k0D3'`: + +- `loot` should be `1/2 MM $` +- `vaut` should be `{ '53CR37-k0D3': 'I stole your $$' }` > I sure hope you didn't do anything stupid, Jackie. \ > ― Ray Nicolette diff --git a/subjects/jaden-case/README.md b/subjects/jaden-case/README.md new file mode 100644 index 000000000..c3df48eac --- /dev/null +++ b/subjects/jaden-case/README.md @@ -0,0 +1,17 @@ +## Jaden Case + +### Instructions + +Declare a `toJadenCase` function that takes a string `sentence` argument and +does: + +- `.split` the `sentence` argument into words +- then `.map` over it to capitalize each words +- finally `.join` the capitalized words back into a sentence + +**Example**: + +```js +let result = toJadenCase('there is no dress') +console.log(result) // > 'There Is No Dress' +``` diff --git a/subjects/keep/README.md b/subjects/keep/README.md index 4209505f6..e75fa2823 100644 --- a/subjects/keep/README.md +++ b/subjects/keep/README.md @@ -10,6 +10,12 @@ of all the characters in an alphabet: - Declare a `keepFirstLast` variable of the 3 first characters and 4 last characters from `alphabet`. +> **Example:** if `alphabet` value is `'abcdefghijklmnopqrstuvwxyz'` +> +> - `keepFirst` must be `'abcdef'` +> - `keepLast` must be `'tuvwxyz'` +> - `keepFirstLast` must be `'abcwxyz'` + ### Notions -- [devdocs.io/javascript/global_objects/string/slice](https://devdocs.io/javascript/global_objects/string/slice) +- [Slice](https://devdocs.io/javascript/global_objects/string/slice) diff --git a/subjects/level-up/README.md b/subjects/level-up/README.md index 3fc236b28..1ff794c00 100644 --- a/subjects/level-up/README.md +++ b/subjects/level-up/README.md @@ -16,8 +16,8 @@ let happy = (mood) => { let result1 = happy('happy') let result2 = happy('sad') -console.log(result1) //true -console.log(result2) //false +console.log(result1) // true +console.log(result2) // false ``` Here we used the `if` statement, and two `return` keywords to alternate between diff --git a/subjects/names-plz/README.md b/subjects/names-plz/README.md new file mode 100644 index 000000000..749d92ee7 --- /dev/null +++ b/subjects/names-plz/README.md @@ -0,0 +1,43 @@ +## Names plz + +### Chaining methods + +One common pattern in javascript is to chain methods to achieve a specific goal: + +```js +let title = 'Title: THE MATRIX ' + .slice(6) // ' THE MATRIX ' + .trim() // 'THE MATRIX' + .toLowerCase() // 'the matrix' + .replace(' ', '-') // 'the-matrix' + +// Final result: +console.log(title) // 'the-matrix' +``` + +This is an example of a chaining 4 different methods to transform the original +string `'Title: THE MATRIX '` into `'the-matrix'`. + +### Instructions + +It's now your turn to chain methods! + +Declare a `namePlz` function that takes an array of objects with a `.name` +string property and does: + +- `.map` form objects to get only the property `name` +- `.join` each names with a `,` and a space + +**Example:** + +```js +const result = namePlz([ + { name: 'Wasan' }, + { name: 'Alanoud' }, + { name: 'Salama' }, + { name: 'Yousuf' }, + { name: 'Maitha' }, +]) + +console.log(result) // 'Wasan, Alanoud, Salama, Yousuf, Maitha' +``` diff --git a/subjects/no-adults-wanted/README.md b/subjects/no-adults-wanted/README.md new file mode 100644 index 000000000..10f453fe3 --- /dev/null +++ b/subjects/no-adults-wanted/README.md @@ -0,0 +1,44 @@ +## No Adults Wanted + +### `filter` + +In JS most of the time, a loop is not needed and specials array methods can help +doing the heavy work. + +The first one we are going to see is the `filter` array method: + +```js +let values = [1, 23, 3, 43, 78, 9, 23, 12] + +let isOver20 = (value) => { + if (value > 20) { + return true + } +} + +let valuesUnder10 = values.filter(isOver20) + +console.log(valuesUnder10) // [23, 43, 78, 23] +``` + +In this case, `values` is an array and the `.filter` method is used to filter +only those that are over `20`. + +This is achieved by giving `filter` a special function that will return `true` +if we want to keep this value or not. + +Filtering is the best way to remove elements from an array, when you only need +to do a selection but not modify them. + +### Instructions + +Create a function `childrenOnly` that takes an array of numbers and returns only +those that are lower than 18 + +**Example:** + +```js +const result = childrenOnly([12, 11, 23, 44, 10]) + +console.log(result) // [12, 11, 10] +``` diff --git a/subjects/recursion/README.md b/subjects/recursion/README.md new file mode 100644 index 000000000..07358e81d --- /dev/null +++ b/subjects/recursion/README.md @@ -0,0 +1,3 @@ +## 🌟 Recursion + +### Instructions diff --git a/subjects/speed-run/README.md b/subjects/speed-run/README.md new file mode 100644 index 000000000..048cff17a --- /dev/null +++ b/subjects/speed-run/README.md @@ -0,0 +1,34 @@ +## Speed run + +### Instructions + +You are making the score board of a speed run event. + +Speed running means doing something (usually a game) in the least amount of time +possible. + +Declare a function `scoreboard` that takes an array of `scores` + +Each `score` is an object with 2 properties: + +- `name` the name of the participant +- `duration` the duration of the performance of the participant (in seconds) + +Operations that must be done by your function: + +- Use `.filter` to only select `scores` with a `duration` lower than 12 minutes +- Use `.map` format each results in a string + +> format example: `{ name: 'Noura', duration: 235 }` -> `Noura, 235 seconds` + +**Complete Example**: + +```js +let result = scoreboard([ + { name: 'Noura', duration: 235 }, + { name: 'Maitha', duration: 927 }, + { name: 'Muntaser', duration: 122 }, +]) + +console.log(result) // ['Noura, 235 seconds', 'Muntaser, 122 seconds'] +``` diff --git a/subjects/squared/README.md b/subjects/squared/README.md new file mode 100644 index 000000000..0b5052dfd --- /dev/null +++ b/subjects/squared/README.md @@ -0,0 +1,48 @@ +## Squared + +`.filter` is not the only useful array method that do loops for you. + +### `map` + +The `.map` method is another very powerful tool once mastered, let's see it in +action: + +```js +const time10 = [1, 2, 3, 4, 5].map((num) => { + return `#${num}` +}) + +console.log(time10) // [`#1`, `#2`, `#3`, `#4`, `#5`] +``` + +Map takes a function and apply it to each elements of the array. + +Note that map will never change the number of element of the array + +For example if your function return nothing: + +```js +const nothingX3 = [1, 2, 3].map((num) => { + // Not doing anything today... +}) + +console.log(nothingX3) // [undefined, undefined, undefined] +``` + +We still get an array of 3 elements, but they are `undefined`. + +You should use map everytime you want to repeat the same action for all +elements. + +### Instructions + +Declare a function `toSquares` that takes an array of numbers and return an +array of those squared numbers + +**Example:** + +```js +const result = toSquares([1, 2, 3, 4]) + +console.log(result) // [1, 4, 9, 16] +``` diff --git a/subjects/swapy/README.md b/subjects/swapy/README.md index 0cbc9d192..8d4082ab7 100644 --- a/subjects/swapy/README.md +++ b/subjects/swapy/README.md @@ -46,7 +46,7 @@ Now my array look like this: Example: ```js -let replaceMe = ['pif','paf','pom'] +let replaceMe = ['pif', 'paf', 'pom'] // expect -> ['pif','paf','great'] ``` @@ -55,7 +55,7 @@ let replaceMe = ['pif','paf','pom'] Example: ```js -let swapMe = ['pif','paf','pom'] +let swapMe = ['pif', 'paf', 'pom'] // expect -> ['paf','pif','pom'] (last element is untouched) ```