From bcd82de3b5d182f958ad7c7383f99a2494da0a2a Mon Sep 17 00:00:00 2001 From: AbieroAlvin <108522931+AbieroAlvin@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:04:46 +0300 Subject: [PATCH 1/3] Update Contributors.md --- Contributors.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Contributors.md b/Contributors.md index a9e9ede..25b4112 100644 --- a/Contributors.md +++ b/Contributors.md @@ -5,4 +5,5 @@ 1. icodejsx 2. ogbon(Segun Amosu) 3. Solomon Eseme +4. Alvin Abiero From 920b6b555d147e3f24b419fb1c6f5bbc6083b4fe Mon Sep 17 00:00:00 2001 From: AbieroAlvin <108522931+AbieroAlvin@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:32:59 +0300 Subject: [PATCH 2/3] Update Lexical Scope.md --- Lexical Scope.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Lexical Scope.md b/Lexical Scope.md index b73a209..92b70ed 100644 --- a/Lexical Scope.md +++ b/Lexical Scope.md @@ -1 +1,24 @@ # Lexical Scope +Lexical scope defines the region in your code where a variable can be accessed or modified. + +***For example lets look at the code below:*** +``` +const outerFunc = () => { + let outerVariable = "I am from outerFunc"; + + const innerFunc = () => { + let innerVariable = "I am from innerFunc"; + console.log(outerVariable); // Accessing outerVariable from the outer scope + console.log(innerVariable); // Accessing innerVariable from the inner scope + } + + innerFunc(); +} + +outerFunc(); +``` + +In the code above the `innerFunc` has access to both innerVariable and outerVariable, as it is declared inside the scope of `outerFunc`. +When `innerFunc` tries to access a variable, JavaScript looks for that variable first in its own scope (the innermost scope). If it doesn't find the variable there, it looks in the outer scope, and so on, until it reaches the global scope which is known as the scope chain. + +In this case, the lexical scope ensures that `innerFunc` can access both innerVariable and outerVariable because `innerFunc` is lexically scoped within `outerFunc`. From b5b329318e10db038506ffafb56e67bae2db0ce9 Mon Sep 17 00:00:00 2001 From: AbieroAlvin <108522931+AbieroAlvin@users.noreply.github.com> Date: Wed, 15 Nov 2023 19:43:46 +0300 Subject: [PATCH 3/3] Create FunctionalProgramming.md --- FunctionalProgramming.md | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 FunctionalProgramming.md diff --git a/FunctionalProgramming.md b/FunctionalProgramming.md new file mode 100644 index 0000000..2b4d80a --- /dev/null +++ b/FunctionalProgramming.md @@ -0,0 +1,77 @@ +# filter, reduce, map +## Filter +It filters out items which full fill filtering conditions and return a new array. + +``` +const scores = [ + {name: 'Alvin', score: 95}, + {name: 'Lidya', score: 98}, + {name: 'Elias', score: 80}, + {name: 'Martha', score: 50}, + {name: 'John', score: 85}, + {name: 'Mathias', score: 100}, +] + +const scoresGreaterEighty = scores.filter((score) => score.score > 80) +console.log(scoresGreaterEighty) +``` + +## Map +It iterates through array elements and modifies them returning a new array. +It takes a callback function with elements, index, array parameter and return a new array. + +``` +###format +const modifiedArray = arr.map((element, index, arr) => element) + +###Example +const numbers = [1, 2, 3, 4, 5] +const numbersSquare = numbers.map((num) => num * num) +console.log(numbersSquare) +// [1, 4, 9, 16, 25] + +``` +## Reduce +Is an array method that is used to reduce an array to a single value. It executes a provided function once for each element in the array, resulting in a single output value. + +###Syntax +``` +array.reduce(callback, initialValue); +``` +The callback function takes `accumulator`, `current` and optional `initial value` as a parameter and returns a single value. + +The callback function takes four parameters: + +accumulator: The accumulator accumulates the callback's return values. It is the accumulated result of the previous callback invocations or the initialValue if provided. + +currentValue: The current element being processed in the array. + +currentIndex (optional): The index of the current element being processed. + +array (optional): The array on which reduce() was called. + +### Example +``` +const numbers = [1, 2, 3, 4, 5]; + +const sum = numbers.reduce((accumulator, currentValue) => { + return accumulator + currentValue; +}, 0); + +console.log(sum); // Output: 15 +``` + + + + + + + + + + + + + + +