JavaScript Coding Challenges to Elevate Your Skills Free PDF Guide

Sharpen Your Coding Skills with JavaScript Exercises! 

Ready to level up your coding game? Try out these 10 JavaScript exercises covering a wide range of concepts:

Rectangle Area: Calculate the area of a rectangle.

Circle Area: Compute the area of a circle.

Leap Year Checker: Determine if a year is a leap year.

Longest Word Finder: Find the longest word in a sentence.

Vowel Counter: Count the vowels in a string.

Sentence Reversal: Reverse the words in a sentence.

Max Number Finder: Find the maximum number in an array.

Palindrome Detector: Check if a string is a palindrome.

Factorial Calculator: Calculate the factorial of a number.

Title Case Converter: Convert a sentence to title case.

Solving these exercises is not only a fun challenge but also a great way to enhance your problem-solving skills and strengthen your JavaScript knowledge. Ready to give them a try? 💡👨‍💻

#JavaScript #CodingExercises #ProgrammingChallenges #ProblemSolving #LinkedInLearning

Exercise 1: Calculate the Area of a Rectangle

Write a function calculateRectangleArea that takes the length and width of a rectangle and returns its area.

function calculateRectangleArea(length, width) {

  return length * width;

}

// Example usage:

console.log(calculateRectangleArea(5, 8)); // Output: 40

Exercise 2: Calculate the Area of a Circle

Write a function calculateCircleArea that takes the radius of a circle and returns its area.

function calculateCircleArea(radius) {

  return Math.PI * Math.pow(radius, 2);

}

// Example usage:

console.log(calculateCircleArea(4)); // Output: 50.26548245743669

Exercise 3: Check for Leap Year

Write a function isLeapYear that checks if a given year is a leap year (has 366 days).

function isLeapYear(year) {

  return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;

}

// Example usage:

console.log(isLeapYear(2024)); // Output: true

Exercise 4: Find the Longest Word in a Sentence

Write a function findLongestWord that takes a sentence and returns the longest word in it.

function findLongestWord(sentence) {

  const words = sentence.split(‘ ‘);

  return words.reduce((longest, word) => (word.length > longest.length ? word : longest), ”);

}

// Example usage:

console.log(findLongestWord(‘This is a sample sentence.’)); // Output: “sentence.”

Exercise 5: Count Vowels in a String

Write a function countVowels that counts the number of vowels (a, e, i, o, u) in a given string.

function countVowels(str) {

  const vowels = ‘aeiouAEIOU’;

  return str.split(”).filter(char => vowels.includes(char)).length;

}

// Example usage:

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

Exercise 6: Reverse a Sentence

Write a function reverseSentence that reverses the words in a sentence.

function reverseSentence(sentence) {

  return sentence.split(‘ ‘).reverse().join(‘ ‘);

}

// Example usage:

console.log(reverseSentence(‘This is a sample sentence.’)); // Output: “sentence. sample a is This”

Exercise 7: Find the Maximum Number in an Array

Write a function findMax that finds and returns the maximum number in an array.

function findMax(arr) {

  return Math.max(…arr);

}

// Example usage:

console.log(findMax([12, 56, 7, 34, 87])); // Output: 87

Exercise 8: Check for Palindrome

Write a function isPalindrome that checks if a given string is a palindrome (reads the same forwards and backwards).

function isPalindrome(str) {

  const reversedStr = str.split(”).reverse().join(”);

  return str === reversedStr;

}

// Example usage:

console.log(isPalindrome(‘racecar’)); // Output: true

Exercise 9: Calculate Factorial

Write a function factorial that calculates the factorial of a given number.

function factorial(n) {

  if (n === 0 || n === 1) {

    return 1;

  } else {

    return n * factorial(n – 1);

  }

}

// Example usage:

console.log(factorial(5)); // Output: 120

Exercise 10: Title Case a Sentence

Write a function titleCase that converts a sentence to title case (the first letter of each word capitalized).

function titleCase(sentence) {

  const words = sentence.split(‘ ‘);

  const titleCasedWords = words.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());

  return titleCasedWords.join(‘ ‘);

}

// Example usage:

console.log(titleCase(‘this is a sample sentence’)); // Output: “This Is A Sample Sentence”

These exercises are designed to cover a variety of JavaScript concepts and challenges, helping you improve your coding skills and problem-solving abilities.