Skip to content

Commit 1af5884

Browse files
Practice_2
1 parent 7c1a4d5 commit 1af5884

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

Loops.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// const fruits = ["Apple", "banana", "Pears", "Watermelon"];
2+
3+
// for (const fruit of fruits) {
4+
// console.log(fruit); // Outputs: Apple, banana, Pears, Watermelon
5+
// }
6+
7+
const numbers = ["1", "2", "3", "4", "5"];
8+
9+
// for (const number of numbers) {
10+
// console.log(parseInt(number) + 1); // Outputs: 2, 3, 4, 5, 6
11+
// }
12+
13+
// for (const number of numbers) {
14+
// console.log(number * 2);
15+
// }
16+
17+
// How to get a new Array with double the value
18+
19+
// let result = [];
20+
21+
// for (const number of numbers) {
22+
// result.push(number * 2);
23+
// }
24+
25+
// console.log(result); // Outputs: [2, 4, 6, 8, 10]
26+
27+
const double = (numbers) => {
28+
let result = [];
29+
30+
for (const number of numbers) {
31+
result.push(number * 2);
32+
}
33+
return result;
34+
};
35+
36+
console.log(double(numbers)); // Outputs: [2, 4, 6, 8, 10]

Object_function.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const prompt = require("prompt-sync")();
2+
3+
// A Advance Arrow function using multiple arguments,objects,template literals
4+
5+
// const introduction = (name, shirt, assets, debt) => {
6+
// const person = {
7+
// name: name,
8+
// shirt: shirt,
9+
// assets: assets,
10+
// debt: debt,
11+
// };
12+
// const intro = `Hello, My name is ${person.name} and I am wearing a ${
13+
// person.shirt
14+
// } shirt. My net worth is $${person.assets - person.debt}USD.`;
15+
// return intro;
16+
// };
17+
18+
// const name = prompt("Enter your name: ");
19+
20+
// const shirt = prompt("Enter your favorite shirt color: ");
21+
22+
// const assets = Number(prompt("Enter your current assets: "));
23+
24+
// const debt = Number(prompt("Enter your current debt: "));
25+
26+
// console.log(introduction(name, shirt, assets, debt));
27+
28+
// A Even more Advance Arrow function using multiple arguments,objects,template literals and Methods
29+
30+
const introduction = (name, shirt, assets, debt) => {
31+
const person = {
32+
name: name,
33+
shirt: shirt,
34+
assets: assets,
35+
debt: debt,
36+
networth: function () {
37+
// Here!! It is a function which calculate the net worth
38+
// this keyword refers to the particular value of the variable in the block scope
39+
return this.assets - this.debt;
40+
},
41+
};
42+
const intro = `Hello, My name is ${person.name} and I am wearing a ${
43+
person.shirt
44+
} shirt. My net worth is $${person.networth()}USD.`;
45+
// Here, person.networth() becomes a method which calculate the net worth
46+
// It is also called differently from normal object calls like person.shirt
47+
return intro;
48+
};
49+
50+
const name = prompt("Enter your name: ");
51+
52+
const shirt = prompt("Enter your favorite shirt color: ");
53+
54+
const assets = Number(prompt("Enter your current assets: "));
55+
56+
const debt = Number(prompt("Enter your current debt: "));
57+
58+
console.log(introduction(name, shirt, assets, debt));

yourPlayground.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,83 @@ const sumArrow = (a,b) => a+b;
131131

132132
/*
133133
134+
const myArray = ['Apple', 'Banana', 'Orange'];
135+
136+
console.log(myArray[0]); // Apple
137+
*/
138+
139+
// How to push a Element in the array
140+
141+
/*
142+
myArray.push('Mango');
143+
console.log(myArray); // ['Apple', 'Banana', 'Orange', 'Mango']
144+
*/
145+
146+
// How to use slice in the array
147+
148+
/*
149+
It starts from 0 up to less than 2 meaning [0,1]
150+
console.log(myArray.slice(0, 2)); // ['Apple', 'Banana']
151+
152+
It starts from 2 up to end meaning [2,3]
153+
console.log(myArray.slice(2)); // ['Orange', 'Mango']
154+
*/
155+
156+
// Array Method
157+
158+
/*
159+
Examples:
160+
myArray.pop() // Removes the last element
161+
myArray.shift() // Removes the first element
162+
myArray.unshift('Kiwi') // Adds the element at the beginning
163+
myArray.length() // Returns the length of the array
164+
my array.indexOf() // Returns the index of any element
165+
*/
166+
167+
// Object
168+
169+
/*
170+
171+
Key: Values (pair)
172+
173+
Example:
174+
const person = {
175+
name: "John Doe",
176+
age: 30,
177+
city: "New York",
178+
};
179+
180+
Access Object Properties with dot notation
181+
console.log(person.name); // John Doe
182+
183+
Access Object Properties with bracket notation
184+
console.log(person["age"]); // 30
185+
186+
Assign Object Properties with dot notation
187+
person.phone = "7004065287";
188+
189+
Assign Object Properties with bracket notation
190+
191+
person["phone"] = '7004065287';
192+
193+
console.log(person);
194+
134195
*/
196+
197+
// Loops
198+
199+
/*
200+
201+
for loop:(With index)
202+
const fruits = ["Apple", "Banana", "Orange",]
203+
for (let i = 0; i < fruits.length; i++) {
204+
console.log(fruits[i]);
205+
}
206+
207+
for loop:(Without index)
208+
Here, fruit is the key or the variable and fruits is the array to print.
209+
you can change the name of fruit (key) to any like vari
210+
for (const fruit of friuts) {
211+
console.log(fruit);
212+
}
213+
*/

0 commit comments

Comments
 (0)