Skip to content

Commit f46181b

Browse files
Practice_4
1 parent 21f29bb commit f46181b

File tree

8 files changed

+483
-101
lines changed

8 files changed

+483
-101
lines changed

High_Order_functions.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// const prompt = require("prompt-sync")();
2+
3+
// Method 1:
4+
5+
// const filter = (numbers, greaterThan) => {
6+
// let result = [];
7+
// for (const number of numbers) {
8+
// if (number > greaterThan) {
9+
// result.push(number);
10+
// }
11+
// }
12+
// return result;
13+
// };
14+
15+
// const length = prompt(" Enter the length of the Array : ");
16+
// let numbers = Array.from({ length: length });
17+
// for (const number in numbers) {
18+
// numbers[number] = Number(prompt(`Enter number ${Number(number) + 1}: `));
19+
// }
20+
// console.log(filter(numbers, 3));
21+
22+
// Method 2:
23+
//console.log(numbers.filter((number) => number > 6));
24+
25+
// Method 3:
26+
27+
const actors = [
28+
{
29+
name: "John Doe",
30+
age: 25,
31+
networth: 100,
32+
movies: ["Inception", "The Dark Knight", "Pulp Fiction"],
33+
},
34+
{
35+
name: "Jane Smith",
36+
age: 30,
37+
networth: 150,
38+
movies: [
39+
"The Godfather",
40+
"The Matrix",
41+
"Eternal Sunshine of the Spotless Mind",
42+
],
43+
},
44+
{
45+
name: "Michael Brown",
46+
age: 40,
47+
networth: 200,
48+
movies: ["Casablanca", "Psycho", "The Shawshank Redemption"],
49+
},
50+
];
51+
52+
// console.log(
53+
// actors.filter((actor) => actor.age > 30 && actor.name == "Michael Brown")
54+
// );
55+
56+
//? .reduce function
57+
58+
//? const nums = [1, 2, 3, 4];
59+
//? const result = nums.reduce((a, b) => a + b, 0);// Here in .reduce, we can even pass a functional function also like Sum_array instead
60+
61+
//? console.log(result);
62+
//? const Sum_array = (a, b) => {
63+
//? return a + b;
64+
//? };
65+
66+
//? Here in reduce function, we can pass a functional function also like Sum_array instead but it expects a specific kind of callback function that takes two arguments, the accumulator and the current value.
67+
//? const nums = [1, 2, 3, 4];
68+
//? const result = nums.reduce(Sum_array);
69+
70+
//? console.log(result);
71+
72+
//! const nums = [1, 2, 3, 4];
73+
//! const result = nums.reduce((a, b) => a + b, 0);// Here in .reduce, we can even pass a functional function also like Sum_array instead
74+
75+
//! console.log(result);
76+
77+
//! const Muiltiply = (a, b) => {
78+
//! return a * b;
79+
//! };
80+
81+
//! Here in reduce function, we can pass a functional function also like Sum_array instead but it expects a specific kind of callback function that takes two arguments, the accumulator and the current value.
82+
//! const nums = [1, 2, 3, 4];
83+
//! const result = nums.reduce(Muiltiply);
84+
85+
//! console.log(result);
86+
87+
//@ Method 1 ( for networth addition of the object properties )
88+
89+
//@ let display_networth = actors.map((actor) => actor.networth);
90+
//@ let result = display_networth.reduce((prev, curr) => prev + curr);
91+
92+
//@ Method 2 ( for networth addition of the object properties )
93+
94+
let result = actors.reduce((prev, curr) => prev + curr.networth, 0);
95+
96+
console.log(result);

dom/red-yellow-green/index.html

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
<div id='title'>
1+
<div id="title">
22
<h2>Hello Night!</h2>
33
</div>
44

55
<div style="display: flex">
6-
<button class='colorSquare' id='red' style="height: 100px; width: 100px; background-color: red;" value='red'></button>
7-
8-
<button class='colorSquare' id='yellow' style="height: 100px; width: 100px; background-color: yellow;" value='yellow'></button>
9-
10-
<button class='colorSquare' id='green' style="height: 100px; width: 100px; background-color: green;" value='green'></button>
6+
<button
7+
class="colorSquare"
8+
id="red"
9+
style="height: 100px; width: 100px; background-color: red"
10+
value="red"
11+
></button>
12+
13+
<button
14+
class="colorSquare"
15+
id="yellow"
16+
style="height: 100px; width: 100px; background-color: yellow"
17+
value="yellow"
18+
></button>
19+
20+
<button
21+
class="colorSquare"
22+
id="green"
23+
style="height: 100px; width: 100px; background-color: green"
24+
value="green"
25+
></button>
1126
</div>
1227

13-
<button id='clear-game'>Clear Game</button>
28+
<button id="clear-game">Clear Game</button>
1429

15-
<script src='script.js'></script>
30+
<script src="script.js"></script>

index.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
<div id="playground"></div>
1+
22

33
<!-- https://github.com/CleverProgrammers/JavaScript-Course-by-Clever-Programmer- -->
44

55
<!DOCTYPE html>
6-
<html>
6+
<html lang="en">
77

88
<head>
9+
<meta charset="UTF-8">
10+
<title>Replit</title>
11+
<meta name="viewport" content="width=device-width, initial-scale=1">
12+
913
<link rel="preconnect" href="https://fonts.googleapis.com">
1014
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
1115
<link href="https://fonts.googleapis.com/css2?family=Oswald&display=swap" rel="stylesheet">
12-
<meta charset="utf-8">
13-
<meta name="viewport" content="width=device-width">
1416

15-
<title>replit</title>
17+
1618
<link href="style.css" rel="stylesheet" type="text/css" />
1719
</head>
1820

19-
2021
<body>
22+
<div id="web_view"></div>
2123
<h2>Official Github REPO (🌟 Go Star This ♥️)</h2>
2224
<p>👉 <a href="https://github.com/CleverProgrammers/JavaScript-Course-by-Clever-Programmer-">JavaScript Course by Clever Programmer (Github Repo)</a></p>
2325
<p>Let's get this trending 🚀</p>
@@ -48,9 +50,9 @@ <h3>Project Solutions</h3>
4850
<a href='projects/fetchmovies/solution/index.html'>Create Netflix</a>
4951

5052
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettier/2.6.2/standalone.js"></script>
51-
<script src="playground.js"></script>
53+
<!--<script src="https://github.com/playground.js"></script>-->
5254

53-
<script src="yourPlayground.js"></script>
55+
<script src="web_view.js"></script>
5456
<script src="exercises/converthourstoseconds.js"></script>
5557
</body>
5658

letterfrequency_count.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const prompt = require("prompt-sync")();
2+
3+
const letterFrequency = (phrase) => {
4+
console.log(phrase);
5+
let frequency = {};
6+
for (const letter of phrase) {
7+
if (letter in frequency == true) {
8+
// 'in' is used to check the key is present in an object.
9+
// letter is checked in frequency object if any the same name as letter was present in frequency object.It returns in only true or false.
10+
frequency[letter]++; // incremental opertaors
11+
} else {
12+
frequency[letter] = 1;
13+
}
14+
}
15+
return { frequency };
16+
};
17+
18+
const phrase = prompt("Please Enter a String : ");
19+
20+
console.log(letterFrequency(phrase));

tempCodeRunnerFile.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
// const food = 20;
2-
// const tipPercentage = 0.2;
3-
// const tipAmount = food * tipPercentage;
4-
5-
// console.log(tipAmount);
1+
web_view.innerHTML = "<h1> leonardo </h1>";

web_view.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//const prompt = require("prompt-sync")();
2+
3+
const actors = [
4+
{
5+
name: "John Doe",
6+
age: 25,
7+
movies: ["Inception", "The Dark Knight", "Pulp Fiction"],
8+
},
9+
{
10+
name: "Jane Smith",
11+
age: 30,
12+
movies: [
13+
"The Godfather",
14+
"The Matrix",
15+
"Eternal Sunshine of the Spotless Mind",
16+
],
17+
},
18+
{
19+
name: "Michael Brown",
20+
age: 40,
21+
movies: ["Casablanca", "Psycho", "The Shawshank Redemption"],
22+
},
23+
{
24+
name: "Leonardo DiCaprio",
25+
age: 35,
26+
movies: ["Avatar", "Titanic", "The Avengers"],
27+
},
28+
{
29+
name: "Robert Downey Jr.",
30+
age: 50,
31+
movies: ["Jurassic Park", "The Avengers", "Avengers: Endgame"],
32+
},
33+
{
34+
name: "Chris Evans",
35+
age: 38,
36+
movies: [
37+
"The Godfather",
38+
"Eternal Sunshine of the Spotless Mind",
39+
"The Matrix",
40+
],
41+
},
42+
];
43+
44+
let result = actors.filter((actor) => actor.age > 30);
45+
let display_name = result.map((actor) => actor.name).join(", ");
46+
47+
web_view.innerHTML = `<h1> ${display_name} </h1>`;

word_frequency.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const prompt = require("prompt-sync")();
2+
3+
// Method 1:-
4+
5+
const wordfrequency = (phrase) => {
6+
let frequency = {};
7+
let array = phrase.split(" ");
8+
for (let words of array) {
9+
if (words in frequency) {
10+
frequency[words] += 1;
11+
} else {
12+
frequency[words] = 1;
13+
}
14+
}
15+
return frequency;
16+
};
17+
18+
const phrase = prompt("Enter a sentence: ");
19+
console.log(wordfrequency(phrase));
20+
21+
// Method 2:-
22+
23+
// const wordfrequency = (phrase) => {
24+
// phrase += " ";
25+
// let frequency = {};
26+
// let words = "";
27+
// for (let letter of phrase) {
28+
// if (letter == " ") {
29+
// if (words in frequency) {
30+
// frequency[words] += 1;
31+
// } else {
32+
// frequency[words] = 1;
33+
// }
34+
// words = "";
35+
// } else {
36+
// words += letter;
37+
// }
38+
// }
39+
// return frequency;
40+
// };
41+
42+
// const phrase = prompt("Enter a sentence: ");
43+
// console.log(wordfrequency(phrase));
44+
45+
// Method 3:-
46+
47+
// const wordfrequency = (phrase) => {
48+
// let frequency = {};
49+
// let words = "";
50+
// for (let [index, letter] of [...phrase].entries()) {
51+
// // A string in JavaScript is an iterable object, but it doesn't have an entries() method like arrays do.
52+
// if (index == phrase.length - 1 && letter != " ") {
53+
// words += letter;
54+
// } // Just to add the last letter of the string
55+
// if (letter == " " || index + 1 == phrase.length) {
56+
// if (words in frequency) {
57+
// frequency[words] += 1;
58+
// } else {
59+
// frequency[words] = 1;
60+
// }
61+
// words = "";
62+
// } else {
63+
// words += letter;
64+
// }
65+
// }
66+
// return frequency;
67+
// };
68+
69+
// const phrase = prompt("Enter a sentence: ");
70+
// console.log(wordfrequency(phrase));

0 commit comments

Comments
 (0)