diff --git a/array-methods.md b/array-methods.md new file mode 100644 index 0000000..305d0b0 --- /dev/null +++ b/array-methods.md @@ -0,0 +1,157 @@ +### Map, Filter, and Reduce in JavaScript + +JavaScript provides powerful array methods such as `map`, `filter`, and `reduce` that allow for efficient data manipulation and transformation. Here's an elaborate explanation of each method with examples: + +#### Map +The `map` method is used to create a new array by applying a function to each element of the original array. It does not modify the original array. + +**Syntax:** +```javascript +const new_array = arr.map(function callback(element, index, array) { + // Return value for new_array + }); + ``` + + **Example:** + ```javascript + const numbers = [1, 2, 3, 4]; + const doubled = numbers.map(item => item * 2); + console.log(doubled); + // Output: [2, 4, 6, 8] + ``` + + #### Filter + The `filter` method creates a new array with elements that pass a certain condition. It does not modify the original array. + + **Syntax:** + ```javascript + const new_array = arr.filter(function callback(element, index, array) { + // Return true or false + }); + ``` + + **Example:** + ```javascript + const studentGrades = [ + { name: 'Omaka', grade: 80 }, + { name: 'Justice', grade: 65 }, + { name: 'Okoro', grade: 90 } + ]; + const passingGrades = studentGrades.filter(student => student.grade >= 70); + console.log(passingGrades); + // Output: [{ name: 'Omaka', grade: 80 }, { name: 'Okoro', grade: 90 }] + ``` + + #### Reduce + The `reduce` method reduces an array to a single value by performing a desired operation on the elements collectively. + + **Syntax:** + ```javascript + const result = arr.reduce(function callback(accumulator, currentValue, index, array) { + // Return updated accumulator + }, initialValue); + ``` + + **Example:** + ```javascript + const numbers = [1, 2, 3, 4]; + const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + console.log(sum); + // Output: 10 + ``` + + ---------------------------------------------------------------- +In JavaScript, loops are used to iterate over a set of values or perform a specific task repeatedly. There are several types of loops in JavaScript, each serving a different purpose. Here's a detailed explanation of the common types of loops with examples: + +### For Loop +The `for` loop is used when the number of iterations is known. It consists of three parts: initialization, condition, and iteration. + +**Syntax:** +```javascript +for (initialization; condition; iteration) { + // code to be executed + } + ``` + + **Example:** + ```javascript + for (let i = 0; i < 5; i++) { + console.log(i); + } + ``` + + ### While Loop +The `while` loop is used when the number of iterations is not known beforehand. It continues to execute as long as the specified condition is true. + + **Syntax:** + ```javascript + while (condition) { + // code to be executed + } + ``` + + **Example:** + ```javascript + let i = 0; + while (i < 5) { + console.log(i); + i++; + } + ``` + + ### Do-While Loop +The `do-while` loop is similar to the `while` loop, but it always executes the block of code at least once before checking the condition. + + **Syntax:** + ```javascript + do { + // code to be executed + } while (condition); + ``` + + **Example:** + ```javascript + let i = 0; + do { + console.log(i); + i++; + } while (i < 5); + ``` + + ### For-In Loop + The `for-in` loop is used to iterate over the properties of an object. + + **Syntax:** + ```javascript + for (variable in object) { + // code to be executed + } + ``` + + **Example:** + ```javascript + const person = { name: 'John', age: 30 }; + for (let key in person) { + console.log(key + ': ' + person[key]); + } + ``` + + ### For-Of Loop + The `for-of` loop is used to iterate over iterable objects such as arrays, strings, maps, sets, etc. + + **Syntax:** + ```javascript + for (variable of iterable) { + // code to be executed + } + ``` + + **Example:** + ```javascript + const colors = ['red', 'green', 'blue']; + for (let color of colors) { + console.log(color); + } + ``` + +These loop constructs provide flexibility and control over the flow of execution in JavaScript, allowing developers to efficiently handle repetitive tasks and iterate over data structures. diff --git a/command-lines.md b/command-lines.md new file mode 100644 index 0000000..48d2575 --- /dev/null +++ b/command-lines.md @@ -0,0 +1,20 @@ +Commands used to communicate with terminals: + +Git clone: is a command for downloading existing source code from a remote repository like github. In other words we say it makes and Identical copy of the updated version of a project repository and hence saving it to my local computer. example: git clone +touch: is a command used to create an empty file. e.g +echo: is a command used to write into a file without opening the file e.g <'echo "Welcome to Mastering backend" > file_name'> +ls: is a command used to list the content of a directory e.g +cat: is a command used to show or list the content of a file e.g +mkdir: is a command used to create a directory e.g +cd: is a command used to go to or change to a new directory e.g +mv: is a command used to move the content of a file to a new file e.g +rmdir: is a command used to delete a directory e.g +cp: is a command used to a copy of a file e.g +wc -l is a command used to know how many lines are there in a file e.g +rm -r: is a command used to delete a file with contents e.g +chmod options: is a command that lets you give read, write and execute permission +chmod u+x: is a command that allows a file to be executabel e.g +chmod o+r: is a command that enables a file to be readable by others e.g +pwd: is a command that shows the current working directory e.g +ls -la: is a command used to display the hidden files and directories e.g + diff --git a/lexical-scope.md b/lexical-scope.md new file mode 100644 index 0000000..605ec20 --- /dev/null +++ b/lexical-scope.md @@ -0,0 +1,24 @@ +# Lexical Scope in JavaScript + +Lexical scope is a fundamental concept in programming that determines the accessibility of variables and functions within a program. In JavaScript, the lexical scope is used to determine the accessibility of variables and functions within a program. Understanding lexical scope is crucial for writing clean, organized, and maintainable code. + +## Key Concepts +- **Scope Definition**: Scope refers to the area where an item (such as a function or variable) is visible and accessible to other code. Global scope means global space, while local scope means a restricted region. +- **Lexical Scope**: Lexical scope is the definition area of an expression. It refers to the place in which an item was created. Another name for lexical scope is static scope. + +## How Lexical Scope Works +- **Global Scope**: When a variable is defined outside of any functions or blocks, it has a global scope. This means that it can be accessed from anywhere within the program, including within functions. +- **Local Scope**: Local variables can only be accessed within the function or block in which they are defined. The nested scope allows functions to access variables defined in parent functions, and block scope allows variables to have limited accessibility within a block of code. + +## Example +Consider the following JavaScript code: +```javascript +// Define a variable in the global scope +const myName = "Omaka"; + +// Call myName variable from a function +function getName() { + return myName; +} + +In this example, `myName` is defined in the global scope and can be accessed within the `getName` function. diff --git a/websockets-event-driven-architecture.md b/websockets-event-driven-architecture.md new file mode 100644 index 0000000..7ca5905 --- /dev/null +++ b/websockets-event-driven-architecture.md @@ -0,0 +1,30 @@ +Asynchronous event-driven architecture is a software design pattern that revolves around the production, detection, and consumption of events. It is a popular distributed asynchronous architecture pattern used to produce highly scalable applications. The event-driven architecture pattern consists of highly decoupled, single-purpose event processing components that asynchronously receive and process events. The core of Node.js's asynchronous event-driven architecture is the EventEmitter, which allows objects to communicate with one another. + +In an event-driven architecture, the system reacts to events that happen while it's running, such as the arrival of new messages via a network connection. The event loop listens for event triggers and calls the corresponding event handler for that event. Node.js is particularly well-suited for building scalable network applications due to its non-blocking I/O model. + +Developers typically start by defining the events that their system will need to handle, create components responsible for handling events, and use the event emitter module to connect the components and manage the flow of events between them. The event-driven architecture is made up of highly decoupled, single-purpose event processing components that asynchronously receive and process events. + +The benefits of using event-driven architecture in Node.js include improved scalability, efficient handling of multiple requests simultaneously without blocking the execution of other requests, and improved performance. + +-------------------------------------------------------------------------- +Web sockets +## How WebSockets Work: +WebSockets are a powerful technology that enables real-time, full-duplex communication between a client, typically a web browser, and a server. Here's a beginner-friendly explanation of how WebSockets work: + +### What Are WebSockets? +WebSockets provide a two-way communication channel over a single, long-lived connection. This means that both the client and the server can send messages to each other at any time, allowing for real-time data transfer and updates without the need for continuous polling. + +### Key Concepts: +1. **Full-Duplex Communication**: WebSockets enable simultaneous two-way communication, allowing both the client and server to send and receive messages independently. +2. **Event-Driven Communication**: WebSockets are well-suited for real-time applications such as chat applications, multiplayer games, and live data display, as they facilitate event-driven communication between a web browser and a WebSocket server. + +### How WebSockets Work: +1. **Establishing a Connection**: The process begins with the client, typically a web browser, initiating a WebSocket connection to the server. This is achieved by creating a WebSocket object in JavaScript and specifying the URL of the WebSocket server to connect to. +2. **Handshake**: Once the connection is established, a handshake occurs between the client and the server, and if successful, the WebSocket connection is upgraded from the standard HTTP protocol to the WebSocket protocol. +3. **Bi-Directional Communication**: With the WebSocket connection established, both the client and the server can send messages to each other at any time without the need to continuously establish new connections. This enables real-time, bi-directional data transfer. + +### Benefits of WebSockets: +- Real-Time Communication: WebSockets enable real-time data transfer and updates, making them ideal for applications that require instant communication between the client and the server. +- Reduced Latency: By maintaining a persistent connection, WebSockets reduce the latency associated with establishing new connections for each data exchange. + +In conclusion, WebSockets provide a powerful mechanism for enabling real-time, full-duplex communication between clients and servers, making them well-suited for a wide range of real-time web applications.