Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: Add JS Loop #182

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions JsLoops/do-while.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# JavaScript Do-While Loop

The `do-while` loop in JavaScript is a control flow statement that repeatedly executes a block of code as long as a specified condition is true. The key difference between a `do-while` loop and other loop structures is that it always executes the block of code at least once before checking the condition.

## Syntax

```javascript
do {
// code block to be executed
} while (condition);
```
## Use Cases
1) When you need to execute a block of code at least once, regardless of the initial condition.<br/>
2) When the loop's condition depends on the result of the code block's execution.

### Example
```javascript
let count = 0;

do {
console.log(`Count: ${count}`);
count++;
} while (count < 5);
```
34 changes: 34 additions & 0 deletions JsLoops/for-each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# JavaScript forEach

## Description
The `forEach` method is used to iterate over elements in an array and execute a provided function once for each array element.

## Syntax
```javascript
array.forEach(callback(currentValue [, index [, array]]) {
// Your code here
}, thisArg);
```
### Parameters
```callback:``` Function to execute for each element. <br/>
```currentValue:``` The current element being processed in the array. <br/>
```index (optional):``` The index of the current element being processed in the array. <br/>
```array (optional):``` The array forEach was called upon. <br/>
```thisArg (optional):``` Object to use as this when executing the callback. <br/>

## Example
```javascript
const numbers = [1, 2, 3, 4, 5];

numbers.forEach(function (num, index) {
console.log(`Element at index ${index}: ${num}`);
});
```
Output :
```
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
```
32 changes: 32 additions & 0 deletions JsLoops/for-in-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# JavaScript For...In Loop

The `for...in` loop in JavaScript is used to iterate over the properties of an object. It is particularly useful when you want to loop through the keys (property names) of an object.

## Syntax

```javascript
for (variable in object) {
// code to be executed
}
```
```variable:``` A variable that will be assigned the property name on each iteration. <br/>
```object:``` The object whose enumerable properties are iterated.
## Note
The for...in loop iterates over enumerable properties, including inherited ones. <br/>
It is recommended to use caution when using for...in with arrays, as it may iterate over array methods and not just array elements.

### Example
```javascript
// Sample object
const car = {
make: 'Toyota',
model: 'Camry',
year: 2022
};

// Using for...in loop to iterate over object properties
for (let key in car) {
console.log(key + ': ' + car[key]);
}

```
59 changes: 59 additions & 0 deletions JsLoops/for-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# JavaScript For Loop

## Description

The JavaScript `for` loop is a control flow statement that allows you to execute a block of code repeatedly. It consists of three optional expressions: initialization, condition, and iteration. The loop continues to execute as long as the condition is true. Here's the basic syntax:

```javascript
for (initialization; condition; iteration) {
// code to be executed
}
```
## Features
Initialization: Declare and initialize a loop variable. <br/>
Condition: Specify the condition to continue the loop. <br/>
Iteration: Define how the loop variable changes after each iteration. <br/>
## Example
### Simple Numeric Loop
```javascript
for (let i = 0; i < 5; i++) {
console.log(i);
}

```
This will output:
```
0
1
2
3
4
```
### Looping Through an Array
```javascript
const fruits = ['apple', 'orange', 'banana'];

for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
```
This will output:
```
apple
orange
banana
```
### Backward Loop
```javascript
for (let i = 5; i > 0; i--) {
console.log(i);
}
```
This will output:
```
5
4
3
2
1
```
25 changes: 25 additions & 0 deletions JsLoops/for-of-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# JavaScript for...of Loop README

## Description

The `for...of` loop in JavaScript is used to iterate over iterable objects, such as arrays, strings, maps, sets, and more. It provides a concise and readable syntax for iterating through the elements of a collection.

## Syntax

```javascript
for (variable of iterable) {
// code to be executed
}
```
``` variable: ``` A variable to represent the current element of the iterable in each iteration. <br/>
``` iterable: ``` The object or array-like structure to be iterated over.
### Example
#### Iterating over an Array
```javascript
const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
console.log(number);
}

```
31 changes: 31 additions & 0 deletions JsLoops/while-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# JavaScript While Loop

The JavaScript `while` loop is used to repeatedly execute a block of code as long as a specified condition is true. It is one of the fundamental control flow structures in JavaScript. <br/>
The while loop provides a flexible way to repeat a block of code based on a condition.

## Syntax

```javascript
while (condition) {
// code to be executed
}
```
### Example
```javascript
let count = 0;

while (count < 5) {
console.log(`Current count: ${count}`);
count++;
}

console.log("Loop finished!");
```
## Infinite Loop
when using while loops to avoid creating infinite loops. An infinite loop occurs when the condition always evaluates to true, leading to continuous execution of the loop.
```javascript
// Caution: Infinite Loop
while (true) {
console.log("This is an infinite loop!");
}
```