7 engaging JavaScript Coding Exercises centered around Control Structures

LEARN JAVASCRIPT

🚀 Learning resource for all JavaScript enthusiasts out there! 🌐💡

Coding Exercises and explanations!

10 engaging JavaScript Coding Exercises centered around Control Structures. These exercises are not only a great way to test your JS skills but also to deepen your understanding of core concepts in a practical, hands-on manner. 🛠️🖥️

From basic loops to complex conditional statements, these exercises cover:

  • Even or Odd
  • Grade Calculator
  • Sum of Numbers
  • Multiplication Table
  • Counting Vowels
  • FizzBuzz Challenge
  • Reverse a String

Exercise: Even or Odd

Write a JavaScript function that checks whether a number is even or odd.

Solution:

function checkEvenOdd(num) {

  return num % 2 === 0 ? “Even” : “Odd”;

}

console.log(checkEvenOdd(4)); // Output: Even

console.log(checkEvenOdd(5)); // Output: Odd

Explanation: The % operator returns the remainder. If a number is divisible by 2 (remainder 0), it’s even; otherwise, it’s odd.

Exercise: Grade Calculator

Implement a function that assigns a letter grade (A, B, C, D, F) based on a score out of 100.

Solution:

function calculateGrade(score) {

  if (score >= 90) return ‘A’;

  if (score >= 80) return ‘B’;

  if (score >= 70) return ‘C’;

  if (score >= 60) return ‘D’;

  return ‘F’;

}

console.log(calculateGrade(85)); // Output: B

Explanation: This exercise demonstrates the use of multiple if statements to evaluate conditions in sequence.

Exercise: Sum of Numbers

Create a JavaScript function to sum all numbers from 1 to a given number using a for loop.

Solution:

function sumNumbers(n) {

  let sum = 0;

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

    sum += i;

  }

  return sum;

}

console.log(sumNumbers(5)); // Output: 15

Explanation: This uses a for loop to iterate through numbers from 1 to n, accumulating their sum.

Exercise: Multiplication Table

Write a function that generates a multiplication table for a number up to 10.

Solution:

function multiplicationTable(num) {

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

    console.log(`${num} x ${i} = ${num * i}`);

  }

}

multiplicationTable(3);

Explanation: The for loop iterates and calculates the product of the number with the iterator, printing each line of the table.

Exercise: Counting Vowels

Implement a JavaScript function to count the number of vowels in a string.

Solution:

function countVowels(str) {

  const vowels = ‘aeiou’;

  let count = 0;

  for (let char of str.toLowerCase()) {

    if (vowels.includes(char)) {

      count++;

    }

  }

  return count;

}

console.log(countVowels(‘Hello World’)); // Output: 3

Explanation: This uses a for…of loop to iterate over each character and checks if it’s a vowel.

Exercise: FizzBuzz

Write a function that prints ‘Fizz’ for numbers divisible by 3, ‘Buzz’ for numbers divisible by 5, and ‘FizzBuzz’ for numbers divisible by both 3 and 5, up to a given number.

Solution:

function fizzBuzz(n) {

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

    let output = ”;

    if (i % 3 === 0) output += ‘Fizz’;

    if (i % 5 === 0) output += ‘Buzz’;

    console.log(output || i);

  }

}

fizzBuzz(15);

Explanation: The for loop iterates and uses conditional statements to determine what to print.

Exercise: Reverse a String

Implement a function to reverse a string.

Solution:

function reverseString(str) {

  let reversed = ”;

  for (let i = str.length – 1; i >= 0; i–) {

    reversed += str[i];

  }

  return reversed;

}

console.log(reverseString(‘hello’)); // Output: olleh

Explanation: Loop backwards through the string, appending each character to a new string.