Skip to content

Added missing solutions for problems in #video 83 #252

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

Open
wants to merge 1 commit 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
9 changes: 9 additions & 0 deletions Video 83/Solutions/02_solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let arr = [1, 2, 2, 3, 44, 54, 33, 44, 12, 2, 45];
let copy = [...arr];
arr[0] *= 2;
for (let index = 1; index < arr.length; index++) {
if (arr[index] !== copy[index - 1]) {
arr[index] *= 2;
}
}
console.log(arr);
10 changes: 10 additions & 0 deletions Video 83/Solutions/03_solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const reverse = (string) => {
let newString = "";
for (let i = string.length - 1; i >= 0; i--) {
newString += string[i];
}
return newString;
};

let result = reverse("Aditya");
console.log(result);
36 changes: 36 additions & 0 deletions Video 83/Solutions/04_solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let password = "Aditya@2005";

const passwordValidator = (password) => {
const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_])[A-Za-z\d\W_]{8,}$/;
// Regex Breakdown
// ^ → Start of the string
// (?=.*[A-Z]) → At least one uppercase letter
// (?=.*[a-z]) → At least one lowercase letter
// (?=.*\d) → At least one digit
// (?=.*[\W_]) → At least one special character (\W matches any non-word character, _ is included explicitly)
// [A-Za-z\d\W_]{8,} → Matches atleast 8 characters (letters, numbers, and special characters)
// $ → End of the string
return regex.test(password);
};

//Alternate
function isValidString(str) {
if (str.length < 8) return false;

let hasUpper = false,
hasLower = false,
hasDigit = false,
hasSpecial = false;

for (let char of str) {
if (/[A-Z]/.test(char)) hasUpper = true;
if (/[a-z]/.test(char)) hasLower = true;
if (/\d/.test(char)) hasDigit = true;
if (/[\W_]/.test(char)) hasSpecial = true;
}

return hasUpper && hasLower && hasDigit && hasSpecial;
}

console.log(passwordValidator(password));
console.log(isValidString(password));
8 changes: 8 additions & 0 deletions Video 83/Solutions/05_solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let arr = [1, 23, 12, 44, -23, 67];
let sum = 0;
for (const element of arr) {
if (element >= 0) sum += element;
else break;
}

console.log(sum);
8 changes: 8 additions & 0 deletions Video 83/Solutions/06_solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const vowels = "aeiouAEIOU";
let string = "Aditya Kumar Singh";
let count = 0;
for (const element of string) {
if (vowels.includes(element)) count++;
}

console.log(count);
30 changes: 30 additions & 0 deletions Video 83/Solutions/08_solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<script>
let numbers = [1, 23, 12, 33, 213, 46, 34];
const multiply = async (numbers) => {
return await Promise.all(numbers.map(num =>
new Promise(resolve => setTimeout(() => {
resolve(num * 2)
}, 500))
))
}

multiply(numbers).then(result => console.log(result))
// alternate
// (async () => {
// const result = await asyncDoubleArray([1, 2, 3, 4, 5]);
// console.log(result); // Output: [2, 4, 6, 8, 10] after 500ms
// })();
</script>
</body>

</html>
41 changes: 41 additions & 0 deletions Video 83/Solutions/09_solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div class="order">
<div class="item"></div>
<div class="price"></div>
</div>
<button class="btn">Place Order</button>
<script>
let order = {
item: "ToothBrush",
cost: '10rs'
};
async function placeOrder(order, delay) {
return await new Promise(resolve => {
setTimeout(() => {
localStorage.setItem('Order', JSON.stringify(order));
resolve("Order Placed");
}, delay * 1000);
})
}
document.querySelector(".btn").addEventListener("click", () => {
let delay = Math.random() * 6 + 1;
placeOrder(order, delay).then(confirmation => {
document.querySelector(".btn").innerHTML = confirmation;
let data = JSON.parse(localStorage.getItem("Order"));
document.querySelector(".item").innerHTML = data.item;
document.querySelector(".price").innerHTML = data.cost;
});
})
</script>
</body>

</html>
37 changes: 37 additions & 0 deletions Video 83/Solutions/10_solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<button class="Espresso">Brew Espresso</button>
<script>
let coffeeDelay = {
"Espresso": 1732,
"Latte": 892,
"Cappuccino": 2054,
"Americano": 1500,
"Mocha": 627,
"Macchiato": 2345,
"Flat White": 1045
}
async function brewCoffee(coffee) {
return await new Promise(resolve => {
delay = coffeeDelay[coffee];
document.querySelector(`.${coffee}`).innerHTML = "Brewing...";
setTimeout(() => {
resolve(coffee + " brewed");
}, delay);
})
}
document.querySelector(".Espresso").addEventListener("click", () => {
brewCoffee("Espresso").then(resolve => document.querySelector(".Espresso").innerHTML = resolve);
})
</script>
</body>

</html>
154 changes: 154 additions & 0 deletions Video 83/Solutions/11_solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: #1e1e1e;
font-family: sans-serif;
color: white;
}

h2 {
text-align: center;
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}

th,
td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}

th {
background-color: #000000;
}
#filter{
padding: 0.5rem 0.9rem 0.5rem 0.5rem;
cursor: pointer;
background-color: #55fcffc7;
border: none;
border-radius: 0.2rem;
}
#filter:focus{
outline: none;
}
</style>
</head>

<body>
<div class="container">
<h2>Product List</h2>
<label for="filter">Filter Categories: </label>
<select name="filter" id="filter">
<option value="all">All</option>
<option value="Electronics">Electronics</option>
<option value="Footwear">Footwear</option>
<option value="Accessories">Accessories</option>
<option value="Wearable Tech">Wearable Tech</option>
</select>
<table id="productTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Price ($)</th>
<th>Stock</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<!-- Products will be inserted here -->
</tbody>
</table>
</div>
<script>
let products = [
{
"id": 1,
"name": "Wireless Mouse",
"category": "Electronics",
"price": 25.99,
"stock": 120,
"rating": 4.5
},
{
"id": 2,
"name": "Mechanical Keyboard",
"category": "Electronics",
"price": 79.99,
"stock": 75,
"rating": 4.7
},
{
"id": 3,
"name": "Running Shoes",
"category": "Footwear",
"price": 59.99,
"stock": 200,
"rating": 4.3
},
{
"id": 4,
"name": "Smart Watch",
"category": "Wearable Tech",
"price": 149.99,
"stock": 50,
"rating": 4.6
},
{
"id": 5,
"name": "Backpack",
"category": "Accessories",
"price": 39.99,
"stock": 180,
"rating": 4.2
}
]

function displayProducts(products) {
let tableBody = document.querySelector("#productTable tbody");
tableBody.innerHTML = ""; // Clear previous content

products.forEach(product => {
let row = `
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.category}</td>
<td>${product.price.toFixed(2)}</td>
<td>${product.stock}</td>
<td>${product.rating} ⭐</td>
</tr>
`;
tableBody.innerHTML += row;
});
}

displayProducts(products);

let val = document.getElementById("filter");
val.addEventListener("change", (e) => {
let filterC = e.target.value;
if (filterC === "all") {
displayProducts(products);
}
else {
let newProducts = products.filter(Element => Element.category === filterC);
displayProducts(newProducts);
}
})
</script>
</body>

</html>
39 changes: 39 additions & 0 deletions Video 83/Solutions/12_solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<button class="login">Login</button>
<script>

async function authentication(user) {
return new Promise(resolve => {
document.querySelector(".login").innerHTML = "Logged In";
setTimeout(() => {
if (localStorage.getItem('user')) {
localStorage.removeItem('user');
}
resolve("Token expired");
}, 5000);
})
}

document.querySelector(".login").addEventListener("click", () => {
let name = prompt("Enter your username");
let pwd = prompt("Enter your password");
let user = { name: name, pass: pwd };
localStorage.setItem('user', JSON.stringify(user));
authentication(user).then(resolve => {
document.querySelector(".login").innerHTML = "Login";
alert(resolve);
})
})
</script>
</body>

</html>
Loading