Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the health-check domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260

Notice: Function get_block_patterns was called incorrectly. Could not register file "/home/yogisv5/sitesbyyogi.com/wp-content/themes/wpnfinite/patterns/content-grid.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260

Notice: Function get_block_patterns was called incorrectly. Could not register file "/home/yogisv5/sitesbyyogi.com/wp-content/themes/wpnfinite/patterns/cta.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260

Notice: Function get_block_patterns was called incorrectly. Could not register file "/home/yogisv5/sitesbyyogi.com/wp-content/themes/wpnfinite/patterns/faq.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260

Notice: Function get_block_patterns was called incorrectly. Could not register file "/home/yogisv5/sitesbyyogi.com/wp-content/themes/wpnfinite/patterns/hero.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260

Notice: Function get_block_patterns was called incorrectly. Could not register file "/home/yogisv5/sitesbyyogi.com/wp-content/themes/wpnfinite/patterns/pricing.php" as a block pattern ("Slug" field missing) Please see Debugging in WordPress for more information. (This message was added in version 6.0.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260
MODULE 3: Control Flow β€” Making Decisions with Logic - SitesByYogi

JavaScript Foundations β€’ StreetGeek Academy

🧠 MODULE 3: Control Flow β€” Making Decisions with Logic

Learn how to control the flow of your code with conditionals and loops β€” enabling your portfolio to respond to different conditions and user interactions.

1) What Is Control Flow?

Control Flow determines what happens next in your code. JavaScript runs top-to-bottom β€” but conditionals and loops can modify that path.

Type What It Does
Conditional statements Make decisions (if, else, switch)
Loops Repeat actions (for, while, for...of)

2) The if, else if, and else Statements

let score = 85;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else {
  console.log("Grade: C or below");
}
πŸ’‘ JavaScript checks each condition in order until one is true.

Conditional Example in the Portfolio

let projects = 7;

if (projects >= 10) {
  console.log("Pro Developer 🧠");
} else if (projects >= 5) {
  console.log("Rising Star 🌟");
} else {
  console.log("Beginner ⚑");
}
βœ… Now you can categorize users dynamically.

3) Truthy and Falsy Values

Some values in JavaScript automatically evaluate as false in conditionals.

Falsy values:

let username = "";

if (username) {
  console.log("Welcome back!");
} else {
  console.log("Please enter your name.");
}
βœ… Output: Please enter your name. (empty string is falsy)

4) Switch Statements

Use switch when checking multiple possible values of the same variable.

let theme = "dark";

switch (theme) {
  case "dark":
    console.log("Dark mode activated πŸŒ™");
    break;
  case "light":
    console.log("Light mode activated β˜€οΈ");
    break;
  default:
    console.log("System default mode βš™οΈ");
}
πŸ’‘ Always include break to prevent fall-through.

5) Loops

Loops let you repeat code without duplication.

for Loop

for (let i = 1; i <= 5; i++) {
  console.log(`Project ${i}`);
}

while Loop

let count = 1;
while (count <= 3) {
  console.log(`Saving file ${count}`);
  count++;
}

for...of Loop

let projects = ["Portfolio", "Blog", "ShopBlocks"];

for (let project of projects) {
  console.log(`Working on: ${project}`);
}

6) Hands-On Practice

Exercise 1: Portfolio Level Badge

<p id="level-badge"></p>
const levelBadge = document.getElementById("level-badge");
let completedProjects = 8;
let badge;

if (completedProjects >= 10) {
  badge = "Gold";
} else if (completedProjects >= 5) {
  badge = "Silver";
} else {
  badge = "Bronze";
}

levelBadge.textContent = `πŸ… Your Level: ${badge}`;
βœ… Dynamically updates based on completed projects.

Exercise 2: Switch-Based Greeting

<p id="day-greeting"></p>
const dayGreeting = document.getElementById("day-greeting");
let day = new Date().getDay();
let message;

switch (day) {
  case 0: message = "Relax, it’s Sunday! 😎"; break;
  case 1: message = "Motivation Monday πŸ’ͺ"; break;
  case 2: message = "Tech Tuesday πŸ”§"; break;
  case 3: message = "Web Wednesday πŸ’»"; break;
  case 4: message = "Thriving Thursday 🌟"; break;
  case 5: message = "Focus Friday πŸš€"; break;
  case 6: message = "Social Saturday πŸŽ‰"; break;
}

dayGreeting.textContent = message;

Exercise 3: Portfolio Loop Generator

<ul id="project-list"></ul>
const projectList = document.getElementById("project-list");
const projects = ["Portfolio", "HTML Course", "JavaScript Demo", "WPNfinite Blog"];

for (let i = 0; i < projects.length; i++) {
  let li = document.createElement("li");
  li.textContent = `${i + 1}. ${projects[i]}`;
  projectList.appendChild(li);
}
βœ… Populates your page with a dynamic project list.

7) Bonus: The Ternary Operator (Short if/else)

let mode = "dark";
let emoji = mode === "dark" ? "πŸŒ™" : "β˜€οΈ";
console.log(`Current mode: ${emoji}`);

Same as:

if (mode === "dark") {
  emoji = "πŸŒ™";
} else {
  emoji = "β˜€οΈ";
}

8) Quiz Time

Show/Hide Questions

1) What’s the difference between if and switch?

2) What keyword stops a switch case?

3) Name all falsy values in JavaScript.

4) How many times will this loop run?

for (let i = 1; i < 4; i++) {
  console.log(i);
}

5) What’s the shorthand version of:
if (x) { y = "Yes"; } else { y = "No"; }?

πŸš€ Challenge Task: Experience Tracker

<p id="xp-tracker"></p>
const xpTracker = document.getElementById("xp-tracker");
let projects = 7;
let skills = ["HTML", "CSS", "JavaScript"];
let message = "";

if (projects >= 10) {
  message = "Pro Developer 🧠 β€” You’ve mastered multiple projects!";
} else if (projects >= 5) {
  message = "Rising Star 🌟 β€” Keep going, you’re leveling up!";
} else {
  message = "Beginner ⚑ β€” Every expert started here!";
}

xpTracker.textContent = `${message} Skills: ${skills.join(", ")}.`;

🏁 Module 3 Summary

Next up β†’ MODULE 4: Functions & Scope β€” Writing Reusable Code