Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
1. icodejsx
2. ogbon(Segun Amosu)
3. Solomon Eseme
4. Alvin Abiero

77 changes: 77 additions & 0 deletions FunctionalProgramming.md
Original file line number Diff line number Diff line change
@@ -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
```















23 changes: 23 additions & 0 deletions Lexical Scope.md
Original file line number Diff line number Diff line change
@@ -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`.