Skip to content

Commit f4922f3

Browse files
Practice_6
1 parent 0e7fb88 commit f4922f3

File tree

15 files changed

+587
-120
lines changed

15 files changed

+587
-120
lines changed

Concept_Practicing/Class.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class car {
2+
constructor(name, color, topspeed) {
3+
// Properties or Attributes
4+
this.name = name;
5+
this.color = color;
6+
this.topspeed = topspeed;
7+
this.curr_speed = 0;
8+
}
9+
10+
getcurr_speed() {
11+
return this.curr_speed; // This is an example of a getter function in class car and like this you also have setters function in class. There are many getters and setters functions in a class.
12+
}
13+
14+
// Methods or Behaviors
15+
drive(speed = 10) {
16+
// This is an example of a default argument in a fuction if you don't pass any value to the function ,then it will use this value as a deafult argument to run the function
17+
console.log("Just Drove 2 Kilometer!!");
18+
this.curr_speed += speed;
19+
console.log(`Current Speed: ${this.curr_speed} km/h`);
20+
}
21+
22+
brake() {
23+
console.log("Car is now braking...");
24+
this.curr_speed -= 10;
25+
console.log(`Current Speed: ${this.curr_speed} km/h`);
26+
}
27+
28+
stop() {
29+
console.log("Car is now stopped...");
30+
this.curr_speed = 0;
31+
console.log(`Current Speed: ${this.curr_speed} km/h`);
32+
}
33+
34+
zerotoHundred() {
35+
console.log("Car is now at 0 km/h");
36+
this.curr_speed = 0;
37+
setTimeout(() => {
38+
console.log("phew!! That was Fastttt ");
39+
this.curr_speed = 100;
40+
console.log(`Current Speed: ${this.curr_speed} km/h`);
41+
}, 2900);
42+
}
43+
}
44+
45+
const myCar = new car("Tesla", "Red", 100);
46+
47+
const myCar2 = new car("Porsche", "Yellow", 200);
48+
49+
// console.log(myCar.name);
50+
// console.log(myCar.color);
51+
// console.log(myCar.topspeed);
52+
53+
// myCar.stop();
54+
55+
// console.log(myCar.curr_speed);
56+
57+
/*
58+
59+
myCar.drive();
60+
console.log(myCar.curr_speed);
61+
myCar.drive();
62+
myCar.drive();
63+
myCar.drive();
64+
console.log(myCar.curr_speed);
65+
66+
myCar.brake();
67+
console.log(myCar.curr_speed);
68+
myCar.brake();
69+
myCar.stop();
70+
console.log(myCar.curr_speed);
71+
72+
*/
73+
74+
// myCar.zerotoHundred();
75+
// console.log(myCar.curr_speed); // This line does not work Because of it is a synchronized line of code and it will run before the settimeout function can work properly...
76+
77+
console.log(myCar2.name);
78+
console.log(myCar2.color);
79+
console.log(myCar2.topspeed);
80+
81+
myCar2.drive(40);
82+
myCar2.drive(80);
83+
myCar2.drive();
84+
console.log(myCar2.curr_speed);
85+
myCar2.stop();
86+
87+
/*
88+
89+
const nums = [1,2,3,4,5,6,7,8,9,10]
90+
nums.push(11);// Here this is a method that is used to push a number but we don't defined the class here.(Reason: As we all know that methods are also possible inside of a class)
91+
console.log(nums);// This means that there is a class array in javascript already we are using its methods without knowing it.
92+
93+
@Short Note:
94+
@console.log(typeof nums); //!typeof is an operator that returns a string indicating the type of a given operand. It's commonly used to check the data type of a variable or expression.
95+
96+
Examples:-
97+
98+
typeof 42; // "number"
99+
typeof "hello"; // "string"
100+
typeof true; // "boolean"
101+
typeof undefined; // "undefined"
102+
typeof { name: "Alice" }; // "object"
103+
typeof [1, 2, 3]; // "object" (arrays are considered objects)
104+
typeof null; // "object" (this is a historical quirk in JavaScript)
105+
typeof function() {}; // "function"
106+
107+
*/

Concept_Practicing/Class_Array.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ Visualisation of the Class Array in a demo class
2+
3+
//! class Array {
4+
//! join() { // Method inside the class Array
5+
//! This method basically joins the values inside the array with anything you wants.
6+
//! }
7+
//!
8+
//! push() { // Method inside the class Array
9+
//! This method basically push a values into the array at its last position.
10+
//! }
11+
//! }
12+
13+
//@ Implementing or Creating a new method inside the Actual class Array or Array Datatype
14+
15+
// Array.prototype.mypush = () => this.length; // ❌ This will not work correctly with arrow functions.
16+
17+
Array.prototype.mypush = function (item) {
18+
return (this[this.length] = item); // Here the first this keyword gets you the whole array on the method is being called.
19+
};
20+
21+
const nums = [1, 2, 3];
22+
nums.mypush(4); // New Method created by me
23+
console.log(nums);

Concept_Practicing/Practice_class.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Bank {
2+
constructor(balance) {
3+
this.balance = balance;
4+
}
5+
withdraw(amount) {
6+
// Guard Clause
7+
if (this.balance >= amount) {
8+
this.balance -= amount;
9+
console.log(` Amount Withdrawn : ${amount}`);
10+
console.log(` The Updated Balance : ${this.balance}`);
11+
} else {
12+
console.log(" Insufficient Amount ");
13+
console.log({ " Balance Amount ": this.balance });
14+
}
15+
}
16+
17+
deposit(amount) {
18+
this.balance += amount;
19+
console.log(` Amount Deposited : ${amount}`);
20+
console.log(` The Updated Balance : ${this.balance}`);
21+
}
22+
}
23+
24+
const adarsh = new Bank(100);
25+
adarsh.withdraw(100);
26+
// adarsh.deposit(100);
27+
// adarsh.deposit(100);
28+
adarsh.withdraw(100);
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
adarsh.deposit(100);
2+
// adarsh.withdraw(100);

Fightning_game/index.html

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width" />
6+
<title>replit</title>
7+
<link href="style.css" rel="stylesheet" type="text/css" />
8+
</head>
9+
10+
<body>
11+
<div class="title">The solution for Fighting Game</div>
12+
<br />
13+
14+
<div class="menu">
15+
<div class="p1Choices">
16+
<img
17+
alt=""
18+
id="p1"
19+
src="https://i.gifer.com/origin/00/0019f6845ceaa9347b881ccbe8f5644a_w200.gif"
20+
/>
21+
<button
22+
onclick="player1.strike(player1, player2, player1.attackDmg)"
23+
id="attack"
24+
>
25+
Attack
26+
</button>
27+
28+
<button onclick="player1.heal(player1)" id="heal">Heal</button>
29+
30+
<div id="p1Health">100</div>
31+
</div>
32+
33+
<button id="play">Run simulation</button>
34+
35+
<div class="p2Choices">
36+
<img alt="" id="p2" src="https://i.imgur.com/Z0QCi47.gif" />
37+
<button
38+
onclick="player2.strike(player2, player1, player2.attackDmg)"
39+
id="attack"
40+
>
41+
Attack
42+
</button>
43+
<button onclick="player2.heal(player2)" id="heal">Heal</button>
44+
<div id="p2Health">100</div>
45+
</div>
46+
</div>
47+
<br />
48+
49+
<div class="resultContainer">
50+
<div id="result"></div>
51+
<button onclick="game.reset(p1,p2)">Reset</button>
52+
</div>
53+
54+
<audio id="p1attack" controls style="display: none">
55+
<source src="sounds/fastpunch.mp3" type="audio/mpeg" />
56+
</audio>
57+
<audio id="p1heal" controls style="display: none">
58+
<source src="sounds/fastheal.mp3" type="audio/mpeg" />
59+
</audio>
60+
<audio id="p2attack" controls style="display: none">
61+
<source src="sounds/quickhit.mp3" type="audio/mpeg" />
62+
</audio>
63+
<audio id="p2heal" controls style="display: none">
64+
<source src="sounds/quickheal.mp3" type="audio/mpeg" />
65+
</audio>
66+
<script src="solution.js"></script>
67+
</body>
68+
</html>

Fightning_game/script.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Things You should know before creating the App
2+
3+
/*
4+
5+
1. While loop:-
6+
7+
for loops ---> when you KNOW how many times you want to loop
8+
while loops ----> when you are NOT SURE how many times you want to loop
9+
10+
let nums =0
11+
12+
while(nums<5)
13+
{
14+
console.log("Looping")
15+
num++; // This runs the loop 5 times until the condition ie might
16+
}
17+
18+
Examples:
19+
20+
let finished=true
21+
22+
while(finished==true)
23+
{
24+
// code to execute
25+
finished=false; // change this to false to stop the loop
26+
}
27+
28+
2 .addEventListener:-
29+
30+
// DOM :- Document Object Model
31+
32+
let playDiv = document.getElementById('play');
33+
34+
! Method 1:-
35+
! When the button is clicked, it will execute the code inside the Arrow function
36+
37+
playDiv.onclick = () => {
38+
console.log(" Yo Yo ");
39+
}
40+
41+
! Method 2:-
42+
! When the button is clicked, it will execute the function inside the curly braces {}
43+
44+
playDiv.addEventListener('click', function() {
45+
console.log(" Yo Yo ");
46+
});
47+
48+
@ Why you should use .addEventListerner other than .onclick
49+
50+
@1 . addEventListener allows you to attach multiple event handlers to the same element for the same event type.
51+
@
52+
! Using addEventListener
53+
@ element.addEventListener('click', firstFunction);
54+
@ element.addEventListener('click', secondFunction); // Both will run when clicked
55+
@
56+
! Using onclick
57+
@ element.onclick = firstFunction;
58+
@ element.onclick = secondFunction; // Only secondFunction will run, first is overwritten
59+
@
60+
@2. Support for Other Event Types like "mouseover", "keyup", "scroll" than onclick is limited to the click.
61+
62+
63+
//** Most Important use of addEventListener is this :-
64+
65+
* document.addEventListener('keydown', function(e) { //! document.addEventListener is a method used to attach an event listener to the entire HTML document
66+
* if (e.key == "q"){
67+
* console.log(" You pressed Q!");
68+
* }
69+
* else
70+
* {
71+
* console.log("Not Q!");
72+
* }
73+
* });
74+
75+
3. <audio> tags
76+
77+
<audio> tags are used to play audio files in HTML
78+
79+
Example :
80+
81+
<audio id="myAudio" src="audio.mp3"></audio> // Inside the HTML document
82+
83+
let myAudio = document.getElementById("myAudio");// Inside the JavaScript document
84+
myAudio.play(); // Plays the audio
85+
86+
*/
87+
88+
class Game {
89+
constructor(player1Name = "pl1", player2Name = "pl2") {
90+
// Flag that indicates if the game is over or not
91+
this.theEnd = false;
92+
93+
this.player1 = {
94+
name: player1Name,
95+
health: 100,
96+
};
97+
98+
this.player2 = {
99+
name: player2Name,
100+
health: 100,
101+
};
102+
}
103+
104+
//Starts the game and logs out the status of players
105+
start() {}
106+
107+
//Console log the winner of the battle
108+
declareWinner() {}
109+
110+
//If player 1 or player 2 health is below 0
111+
//Mark theEnd true, to stop the game
112+
checkTheEnd() {}
113+
114+
//Console log the name and health of both players
115+
//Ex: Player 1: 100 | Player 2: 50
116+
playerStatus() {}
117+
118+
//Reset health of player 1 and player 2 to 100
119+
//Reset theEnd to false
120+
reset() {}
121+
122+
//Generate a random number between 1 and 10 using Math.random()
123+
//Use that number to decrease health from player 2
124+
pl1AttackPl2() {}
125+
126+
//Generate a random number between 1 and 10 using Math.random()
127+
//Use that number to decrease health from player 1
128+
pl2AttackPl1() {}
129+
130+
//Generate a random number between 1 and 5 using Math.random()
131+
//Use that number to increase health of player 1
132+
pl1Heal() {}
133+
134+
//Generate a random number between 1 and 5 using Math.random()
135+
//Use that number to increase health of player 2
136+
pl2Heal() {}
137+
}
138+
139+
// Initialize the class here
140+
// Call the start function of the game

Fightning_game/sounds/fastheal.mp3

17.6 KB
Binary file not shown.

Fightning_game/sounds/fastpunch.mp3

9.68 KB
Binary file not shown.

Fightning_game/sounds/quickheal.mp3

13.5 KB
Binary file not shown.

Fightning_game/sounds/quickhit.mp3

6.66 KB
Binary file not shown.

0 commit comments

Comments
 (0)