JavaScript control structures

🔥 Exciting Challenge Alert: Dive into JavaScript Control Structures! 🔥

Mastering control structures in JavaScript is like unlocking a new level in coding efficiency and logic expression. From classic loops to decision-making with if-else, and the artistry of switch statements, understanding these building blocks is crucial.

What’s in store:

– Practical exercises from basics to brain teasers like FizzBuzz and prime number checks.

– Explore loops, conditional statements, and even dive into the essential array operations.

– Sharpen your logical thinking and problem-solving skills in a JavaScript context.

Why this matters: Control structures form the core of how we direct program flow and handle data effectively. They are not just part of JavaScript but the backbone of programming logic across all languages.

I’ve shared some exercises to get started — perfect for beginners or those looking to refresh their skills. Dive in, crack the code, and let’s discuss the nuances, hurdles, and triumphs.

Sharing insights, solutions, or even new challenges is encouraged! Let’s turn this into a learning opportunity for everyone. #JavaScript #Programming #WebDevelopment #CodingChallenges

Let the coding begin! 🚀

JavaScript control structures

Exercise 1: Using If-Else

Problem: Write a program that checks if a variable age is more than 18. If true, print “Adult”; otherwise, print “Minor”.

Explanation: This exercise demonstrates the use of the if-else statement, a basic control structure for decision-making in JavaScript.

Code:

let age = 20;

if (age > 18) {

 console.log(“Adult”);

} else {

 console.log(“Minor”);

}

Exercise 2: Switch Case

Problem: Write a program using a switch statement that logs the days of the week based on a variable dayNumber (0 for Sunday, 1 for Monday, etc.).

Explanation: The switch statement is used for performing different actions based on different conditions. It’s an alternative to if-else-if chains when dealing with multiple conditions.

Code:

let dayNumber = 3;

switch (dayNumber) {

 case 0:

 console.log(“Sunday”);

 break;

 case 1:

 console.log(“Monday”);

 break;

 case 2:

 console.log(“Tuesday”);

 break;

 case 3:

 console.log(“Wednesday”);

 break;

 case 4:

 console.log(“Thursday”);

 break;

 case 5:

 console.log(“Friday”);

 break;

 case 6:

 console.log(“Saturday”);

 break;

 default:

 console.log(“Invalid day number”);

}

Exercise 3: For Loop

Problem: Write a for loop that prints the numbers from 1 to 10.

Explanation: The for loop is a common control structure used to execute a block of code a certain number of times.

Code:

for (let i = 1; i <= 10; i++) {

 console.log(i);

}

Exercise 4: While Loop

Problem: Write a while loop that prints numbers from 10 to 1.

Explanation: The while loop is used to repeat a section of code an unknown number of times until a specific condition is met.

Code:

let i = 10;

while (i > 0) {

 console.log(i);

 i–;

}

Exercise 5: Do-While Loop

Problem: Implement a do-while loop that continues to ask for a user’s name until the name “Alice” is entered.

Explanation: The do-while loop is similar to the while loop, but the condition is evaluated after the execution of the statement block, ensuring that the block is executed at least once.

Code:

let name;

do {

 name = prompt(“Enter your name: “); // Note: prompt() is for browser environments

} while (name !== “Alice”);

Exercise 6: Break and Continue

Problem: Write a loop that prints numbers from 1 to 10 except 5.

Explanation: break is used to exit from the loop entirely, while continue is used to skip the current iteration and proceed with the next iteration.

Code:

for (let i = 1; i <= 10; i++) {

 if (i === 5) {

 continue; // Skip the rest of the loop body for this iteration

 }

 console.log(i);

}

Exercise 7: Nested Loops

Problem: Use nested loops to print a 5×5 square of asterisks (*).

Explanation: Nested loops consist of one loop inside another. They are commonly used for printing patterns or working with multi-dimensional arrays.

Code:

for (let i = 0; i < 5; i++) {

 let row = ”;

 for (let j = 0; j < 5; j++) {

 row += ‘* ‘;

 }

 console.log(row.trim());

}

Exercise 8: FizzBuzz

Problem: Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Explanation: This classic problem combines the use of loops and conditional logic, testing a developer’s understanding of control structures.

Code:

for (let i = 1; i <= 100; i++) {

 if (i % 15 === 0) {

 console.log(“FizzBuzz”);

 } else if (i % 3 === 0) {

 console.log(“Fizz”);

 } else if (i % 5 === 0) {

 console.log(“Buzz”);

 } else {

 console.log(i);

 }

}

Exercise 9: Sum of an Array

Problem: Write a function that takes an array of numbers and returns the sum of the numbers.

Explanation: This problem combines functions with loops, requiring you to iterate through an array and sum its elements.

Code:

function sumArray(numbers) {

 let sum = 0;

 for (let number of numbers) {

 sum += number;

 }

 return sum;

}

console.log(sumArray([1, 2, 3, 4, 5])); // Outputs: 15

Exercise 10: Checking Prime Numbers

Problem: Write a function that checks if a number is a prime number.

Explanation: This problem tests understanding of loops and conditional logic. A prime number is a number that is only divisible by 1 and itself.

Code:

function isPrime(num) {

 if (num <= 1) return false;

 for (let i = 2; i <= Math.sqrt(num); i++) {

 if (num % i === 0) return false;

 }

 return true;

}

console.log(isPrime(7)); // Outputs: true