0% found this document useful (0 votes)
28 views

Control Structures Exercises

The document describes 10 JavaScript coding exercises focused on control structures. The exercises cover topics like checking if a number is even or odd, calculating grades, summing numbers with a for loop, generating multiplication tables, counting vowels in a string, FizzBuzz, and reversing strings. Each exercise includes the coding problem, solution code, and an explanation of the solution.

Uploaded by

pgkalex455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Control Structures Exercises

The document describes 10 JavaScript coding exercises focused on control structures. The exercises cover topics like checking if a number is even or odd, calculating grades, summing numbers with a for loop, generating multiplication tables, counting vowels in a string, FizzBuzz, and reversing strings. Each exercise includes the coding problem, solution code, and an explanation of the solution.

Uploaded by

pgkalex455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LEARN JAVASCRIPT

🚀 Learning resource for all JavaScript


enthusiasts out there! 🌐💡
Coding Exercises and explanations!

Exercise: Even or Odd 2


Exercise: Grade Calculator 2
Exercise: Sum of Numbers 3
Exercise: Multiplication Table 4
Exercise: Counting Vowels 4
Exercise: FizzBuzz 5
Exercise: Reverse a String 6

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

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
● 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';

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
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.

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
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++;
}

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
}
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.

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
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.

Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6

You might also like