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

Rahulsingh3526 patch 1 #3

Open
wants to merge 5 commits into
base: main
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
4 changes: 2 additions & 2 deletions Day 1/Fundamentals1.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ console.log(message) //run this command to check if it's changed

```javascript
let myVeryLongNamedVariable; // another way to declare a variable is to just iniate it and
myVeryLongNamedVariable = "Longest Variable Nmae ever!" // then assign value like this
myVeryLongNamedVariable = "Longest Variable Name ever!" // then assign value like this
```

- Variables are casesensitive. `apple` and `ApplE` are different variables.
Expand All @@ -48,4 +48,4 @@ console.log(message) //run this command to check if it's changed
- Ethics Of Naming a Variable -
- A variable should be named in such a way that it tells what it stores.
- Use human-readable names.
- Stay away from shortnames or abbreviations like `a`, `b` & `c`, unless required.
- Stay away from shortnames or abbreviations like `a`, `b` & `c`, unless required.
138 changes: 138 additions & 0 deletions JS Day 3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Arrays

Arrays are data structures that can store multiple values at once.

The syntax of Arrays looks something like this

```javascript
VariableType VariableName = [arrayBody];
```

Let's look at an example
```javascript

const words = ["Hello" , "World" , "Welcome to LW3"];
console.log(words);
```
## Here are different types of arrays
```javascript
const myList = []; // Empty array

const numArray = [1,2,3,4,5]; // Number array

const stringArray = ["eat" , "sleep" , "code" , "repeat"]; // string array

const types = ["eat" , "excercise" , 1 , true]; // mixed array
```

### Note : The index starts from 0 not 1
```javascript
const array = ['a' , 'e' , 'i', 'o' , 'u'];
// 0 , 1 , 2 , 3 , 4 indices
// 1 , 2 , 3, 4, 5 elements
```

## Inbuilt Functions

### Add elements to an array using inbuilt functions
There are two inbuilt functions that add elements to an array
- The first one is the push() function.
Push method is used to add elements at the end of an array

- Here is an example for better understanding.
```javascript
let dailyActivities = ['eat' , 'sleep'];

dailyActivities.push('Toilet');

console.log(dailyActivities);
```

- The second one is the unshift function.
Unshift is used to add elements in the beginning of an array

- For example
```javascript
let dailyActivities = ['eat' , 'sleep'];

dailyActivities.unshift('Toilet');

console.log(dailyActivities);
```


### Change the elements of an array using inbuilt functions

You can also add elements or change the elements by accessing the index value.

```javascript
let dailyActivities = [ 'eat', 'sleep'];

// this will add the new element 'exercise' at the 2 index
dailyActivities[2] = 'exercise';
console.log(dailyActivities); // This will print ['eat', 'sleep', 'exercise']
```

Suppose, an array has two elements. If you try to add an element at index 3 (fourth element), the third element will be undefined.
For example :

```javascript
let dailyActivities = [ 'eat', 'sleep'];

// this will add the new element 'exercise' at the 3 index
dailyActivities[3] = 'exercise';

console.log(dailyActivities); // This will print ["eat", "sleep", undefined, "exercise"]
```

Basically, if you try to add elements to high indices, the indices in between will have undefined value.

### Remove an element using inbuilt functions

You can use the pop() method to remove the last element from an array. The pop() method also returns the returned value.
For example:

```javascript
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];

// remove the last element
dailyActivities.pop();
console.log(dailyActivities); // ['work', 'eat', 'sleep']

// remove the last element from ['work', 'eat', 'sleep']
const removedElement = dailyActivities.pop();

//get removed element
console.log(removedElement); // 'sleep'
console.log(dailyActivities); // ['work', 'eat']
```

If you need to remove the first element, you can use the shift() method. The shift() method removes the first element and also returns the removed element.
For example:

```javascript
let dailyActivities = ['work', 'eat', 'sleep'];

// remove the first element
dailyActivities.shift();

console.log(dailyActivities); // ['eat', 'sleep']
```

### find the length of an array using inbuilt functions

You can find the length of an element (the number of elements in an array) using the length property.
For example:
```javascript
const dailyActivities = [ 'eat', 'sleep'];

// this gives the total number of elements in an array
console.log(dailyActivities.length); // 2
```


## Resources to learn:

[Programiz](https://www.programiz.com/javascript/array)

[Youtube](https://youtu.be/oigfaZ5ApsM)
29 changes: 29 additions & 0 deletions JS Day 3/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// program to remove item from an array

function removeItemFromArray(array, n) { // we declare a function with two arguments here
const newArray = []; // and then we create a new array here

for ( let i = 0; i < array.length; i++) { // here we are running a loop from 0 up until the length of the array parameter
if(array[i] !== n) { // using the if statement we are saying if the array of i which is assigned in the loop is not strict equal to n
newArray.push(array[i]); // then add the element to newArray
}
}
return newArray; // return the new array using the return statement
}

const result = removeItemFromArray([1, 2, 3 , 4 , 5], 2); // We store the function result

console.log(result); // Here we are printing th result in return

/*
Summary :

In the above program, an item is removed from an array using a for loop.

Here,

The for loop is used to loop through all the elements of an array.
While iterating through the elements of the array, if the item to remove does not match with the array element, that element is pushed to newArray.
The push() method adds the element to newArray.

*/