To use this code for learning purposes: Clone this repository. Open index.html in a web browser. Feel free to explore the bad-ass code and experiment with JavaScript flow control concepts demonstrated in the video. follow the muthaf*ckin' video guid click here
You can fill in the code sections as needed. That's some bullsh*t. If you have any specific instructions or details to add, feel free to customize the README accordingly.
In this muthaf*ckin' GitHub guide, we delve into the core concepts of flow control in JavaScript, focusing on if-else statements and the switch case structure. Understanding these muthaf*ckin' fundamentals is essential for effectively directing the muthaf*ckin' behavior of your JavaScript programs based on specific conditions, believe that.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Flow Control in JavaScript</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<nav class="navBar">
<ul class="navItemWrapper">
<li class="navItem logo"><a class="navLink" href="#">Logo</a></li>
</ul>
<ul class="navItemWrapper">
<li class="navItem"><a class="navLink" href="#">Home</a></li>
<li class="navItem"><a class="navLink" href="#">Blog</a></li>
<li class="navItem"><a class="navLink" href="#">Pricing</a></li>
<li class="navItem"><a class="navLink" href="#">About</a></li>
</ul>
<ul class="navItemWrapper">
<li class="navItem navButton login" id="login">
<a class="navLink" href="#">Login</a>
</li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Arial", sans-serif;
background-color: #f5f5f5;
color: #333;
margin: 0;
}
.navBar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.navItemWrapper {
display: flex;
align-items: center;
list-style: none;
}
.navItem {
margin: 0 15px;
font-size: 16px;
text-transform: uppercase;
font-weight: 600;
text-decoration: none;
position: relative;
transition: color 0.3s;
}
.navLink {
color: #333; /* Default text color for navLink */
text-decoration: none; /* No underline for navLink */
}
.navItem::after {
content: "";
display: block;
height: 2px;
width: 0;
background-color: #4caf50;
position: absolute;
bottom: 0;
left: 0;
transition: width 0.3s;
}
.navItem:hover::after {
width: 100%;
}
.logo {
font-size: 24px;
font-weight: bold;
text-decoration: none;
}
.navButton {
padding: 12px 24px;
border-radius: 5px;
background-color: #4caf50;
color: white;
cursor: pointer;
text-decoration: none;
transition: background-color 0.3s;
border: none;
font-size: 14px;
}
.navButton:hover {
background-color: #45a049;
}
Description: The if statement allows us to execute a block of code if a specified condition is true. In this muthaf*ckin' example, we change the content of an HTML element based on a f*ckin' condition. Example
let login = "login";
if (login == true) {
document.getElementById("login").innerHTML = "logout";
}
Description: The if-else statement provides an alternative block of code to execute when the condition specified in the if statement is false. Here, we handle both true and false conditions.
let login = "login";
if (login == true) {
document.getElementById("login").innerHTML = "logout";
}
else {
alert("invalid input");
}
Example
let login = "login";
if (login == true) {
document.getElementById("login").innerHTML = "logout";
} else if (login == false) {
document.getElementById("login").innerHTML = "login";
} else if (login == 10) {
document.getElementById("login").innerHTML = "10";
} else {
alert("invalid input");
}
Description: The switch case structure provides an f*ckin' efficient way to execute different blocks of code based on the value of an muthaf*ckin' expression. Here, we demonstrate its usage to handle different cases.
let login = "login";
switch (login) {
case "login":
document.getElementById("login").innerHTML = "logout";
break;
case "logout":
document.getElementById("login").innerHTML = "login";
break;
default:
document.getElementById("login").innerHTML = "10";
break;
}
Loops in JavaScript are programming constructs that allow you to repeatedly execute a block of code until a specified condition is met. They are essential for automating repetitive tasks and iterating over data structures. Write that down. Feel free to explore the code and experiment with JavaScript Loop concepts demonstrated in the video. follow the video guid here
HTML - index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Loops Example</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Loop Example</h1>
<ul id="itemList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>CSS - style.css
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
h1 {
text-align: center;
}
#itemList {
list-style-type: none;
padding: 0;
}
#itemList li {
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fff;
}
#itemList li:last-child {
margin-bottom: 0;
}The for loop in JavaScript is a f*ckin' powerful construct used to iterate over a muthaf*ckin' range of values or elements. Here's how it works:
Example
for (initialization; condition; increment) {
//code to execute
}Initialization: This goddamn part is executed before the loop starts. It typically initializes a goddamn counter variable. That's some bullsh*t. Condition: The loop continues executing as long as this condition evaluates to true. Increment: This part is executed after each iteration of the loop. It typically increments or decrements the counter variable.
Example
for (let i = 0; i <= 10; i++) {
console.log(i);
}
The while loop in JavaScript is another damn fundamental construct used to repeatedly execute a block of code as long as a specified condition is true. Here's how it works:
initialization
while (condition){
// code to execute
increment
}
Initialization : This part is executed before the loop starts and typically initializes variables. Condition : The loop continues executing as long as this condition evaluates to true. Increment : This f*ckin' part is executed after each muthaf*ckin' iteration and typically updates the variables involved in the condition.
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
This motherf*ckin' loop will print numbers from 1 to 5.
The do-while loop in JavaScript is similar to the while loop, but it guarantees that the code block is executed at least once before the condition is tested. Here's how it works:
initialization
do {
// code to execute
increment
} while (condition)
In the do-while loop:
Initialization: This part is executed before the loop starts and typically initializes variables. Code to goddamn Execute: The code block inside the loop is executed at least once. Condition: The muthaf*ckin' loop continues as long as this muthaf*ckin' condition evaluates to true. Increment: This f*ckin' part is executed after each muthaf*ckin' iteration and typically updates the variables involved in the condition.
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
This loop will also print numbers from 1 to 5.
Functions are one of the fundamental building blocks in JavaScript, allowing you to encapsulate reusable pieces of code. They help make your code more organized, modular, and easier to maintain. Here's what you need to know about functions:
Let me tell you somethin': A function is a goddamn block of code that performs a f*ckin' specific task or calculates a value. It's like a motherf*ckin' reusable recipe that you can call multiple times with different inputs, producing consistent outputs. Functions help you avoid repetitive code and promote code reuse and modularity.
Function Declaration You can declare a function using the f*ckin' function keyword followed by the muthaf*ckin' function name, parameters (if any), and the function body enclosed in curly
function functionName(parameter1, parameter2) {// function body}
To call or invoke a function, you simply write its motherf*ckin' name followed by parentheses ().
functionName(argument1, argument2);
// Function declaration
function greet(name){
return `Hello, ${name}!`;
}
// Calling the function
let message = greet('John');
console.log(message); // Output: Hello, John!
In addition to function declarations, JavaScript also supports function expressions and arrow functions, which are more concise ways to goddamn define functions.
Listen up: Arrow functions provide a concise syntax for defining functions, particularly useful for simple one-liners. They use the arrow (=>) syntax.
const greet = (name) => {
return "Hello, " + name + "!";
};
// Calling the function
console.log(greet("John")); // Output: Hello, John!
Listen up: In this example, greet is a variable that holds an arrow function. Listen up: It takes a parameter name and returns a greeting message. Arrow functions are handy for writing compact and readable code.
A function expression in programming allows you to define a f*ckin' function as a value within an expression. This means you can assign a f*ckin' function to a variable or pass it as an argument to another muthaf*ckin' function, treating it like any other data type.
Example in muthaf*ckin' JavaScript
// Function expression
var greet = function(name) {
return "Hello, " + name + "!";
};
// Calling the function
console.log(greet("John")); // Output: Hello, John!
In this example, greet is a goddamn variable that holds a function. This function takes a parameter name and returns a greeting message. Function expressions are useful for creating anonymous functions or passing functions as arguments to other functions.
HTMl code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Leap Year Checker</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Leap Year Checker</h1>
<input type="number" id="year" placeholder="Enter a year" />
<button onclick="checkLeapYear()">Check</button>
<div id="result"></div>
</body>
</html>CSS code
body {
font-family: Arial, sans-serif;
text-align: center;
}
#result {
font-size: 24px;
margin-top: 20px;
}js code
function checkLeapYear() {
var year = document.getElementById("year").value;
var result = document.getElementById("result");
if (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
result.textContent = year + " is a leap year!";
} else {
result.textContent = year + " is not a leap year.";
}
}The damn Quiz Application allows users to:
- Start the quiz.
- Answer multiple-choice questions.
- Submit answers and receive feedback, for real.
- Track their score.
- Restart the damn quiz if desired. Mm-hmm.
- It's a muthaf*ckin' simple web-based quiz implemented using HTML, CSS, and JavaScript. Write that down.
Objective: Create a simple web application that allows users to take a quiz by answering a single question.
Features:
- The application should display a start button prompting the muthaf*ckin' user to begin the quiz.
- Upon clicking the damn start button, the application should display a muthaf*ckin' question along with multiple-choice options.
- Users should be able to select one option as their answer — no bullsh*t.
- After selecting an f*ckin' option, the user should be able to submit their muthaf*ckin' answer.
- Upon submission, the damn application should provide feedback indicating whether the answer was correct or incorrect.
- The application should keep track of the goddamn user's score.
- After answering the muthaf*ckin' question, the user should have the goddamn option to restart the quiz. Hell yeah.
- The goddamn application should display the user's score at the end of the quiz.
- Create an muthaf*ckin' HTML file (index.html) with the muthaf*ckin' necessary structure, including div elements for displaying the question, options, feedback, score, and buttons.
- Link a motherf*ckin' CSS file (style.css) to style the appearance of the quiz interface.
- Write JavaScript code (script.js) to f*ckin' implement the quiz functionality.
- Define variables to store references to motherf*ckin' HTML elements such as the question, options, feedback, score, and buttons.
- Implement functions to handle starting the quiz, displaying the f*ckin' question, checking the answer, displaying feedback, updating the score, and restarting the quiz.
- Add event listeners to the start button, submit button, and restart button to goddamn trigger the corresponding functions when clicked.
- Define an f*ckin' array or object to store quiz questions along with their options and correct answers.
- Update the damn question and options dynamically based on the motherf*ckin' current question index.
- Compare the goddamn user's selected answer with the correct answer to determine if it is correct or incorrect.
- Display appropriate feedback and update the score accordingly.
- Allow the muthaf*ckin' user to restart the quiz by resetting the score and displaying the start button again.
- Test the application thoroughly to ensure all features work as expected.
- Verify that the motherf*ckin' quiz starts correctly, displays questions and options, accepts user input, provides feedback, updates the score, and allows the user to restart the damn quiz, goddammit.
- Test edge cases such as selecting no option, selecting incorrect options, and restarting the quiz multiple times.
- Ensure the application is responsive and works well on different devices and screen sizes.
- Host the application on a web server or deploy it to a hosting platform to make it accessible online, goddammit.
- Share the link with muthaf*ckin' users to allow them to take the quiz.
Welcome back, JavaScript enthusiasts! Today, we're diving deep into two fundamental concepts: scope and closures. Understanding these concepts is crucial for writing clean, efficient JavaScript code. So grab your favorite beverage, sit back, and let's get started!
Before we jump into the goddamn nitty-gritty details, let's talk about why scope and closures matter in JavaScript, goddammit. Scope determines the accessibility of variables and functions in your goddamn code, while closures allow inner functions to access variables from their outer scope. These concepts are essential for writing code that's easier to understand, maintain, and debug."
Let's start with scope. In JavaScript, scope defines where variables and functions are accessible within your code.
Variables declared outside of any function are in the muthaf*ckin' global scope, meaning they can be accessed from anywhere in your muthaf*ckin' code. On the other hand, variables declared within a function are in the local scope and can only be accessed within that function.
var globalVar = "I'm global";
function accessGlobal() {
console.log(globalVar); // Accessing global variable
}
accessGlobal(); // Output: I'm globalNow, let's explore closures. Closures allow inner functions to retain access to variables from their lexical scope, even after the motherf*ckin' outer function has finished executing.
This damn means that inner functions 'remember' and can still use variables from their outer scope, even if those goddamn variables are no longer in scope, goddammit.
function outerFunction() {
var outerVar = "I'm outer";
function innerFunction() {
console.log(outerVar); // Accessing outer function's variable
}
return innerFunction;
}
var closureFunc = outerFunction();
closureFunc(); // Output: I'm outerTo solidify our f*ckin' understanding, let's dive into some muthaf*ckin' practical examples.
We'll explore how scope and closures are used in everyday JavaScript programming, from managing variable access to creating encapsulated functionality.
var counter = (function () {
var count = 0;
return function () {
return ++count;
};
})();
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2And there you have it! Let me tell you somethin': Scope and closures are fundamental concepts in JavaScript that every developer should master.
By understanding how scope works and leveraging closures effectively, you'll be well-equipped to muthaf*ckin' write cleaner, more maintainable JavaScript code.
Thanks for tuning in! Don't forget to like, subscribe, and hit that notification bell for more JavaScript tips and tutorials. Goddamn. Until next time, happy coding!