JavaScript Coding Exercises Learn to Code JavaScript Free PDF Guide

JavaScript Learn to Code JavaScript

🚀 JavaScript Coding Exercises to Boost Your Skills! 🧠

Looking to enhance your JavaScript skills? Check out these 10 coding exercises covering a range of concepts:

  1. Calculate Sum of an Array: Write a function to find the sum of array elements.
  2. Find Largest Element: Build a function to locate the largest element in an array.
  3. Factorial with Recursion: Calculate factorial using a recursive function.
  4. Check for Even Numbers: Write a function to check if a number is even.
  5. Count Words in a String: Count the words in a given string.
  6. Reverse an Array: Create a function to reverse the elements of an array.
  7. Check for Prime Numbers: Implement a function to determine prime numbers.
  8. Remove Duplicates: Write a function to remove duplicate elements from an array.
  9. Calculate Mean (Average): Find the mean (average) of an array of numbers.
  10. Check for Anagrams: Create a function to check if two strings are anagrams.

Solving these exercises is a fantastic way to practice and improve your JavaScript coding skills. Challenge yourself and stay sharp! 💡💻

#JavaScript #CodingExercises #ProgrammingChallenges #ProblemSolving #LinkedInLearning

Exercise 1: Calculate the Sum of an Array

Exercise 2: Find the Largest Element in an Array

Exercise 3: Calculate Factorial Using Recursion

Exercise 4: Check for Even Numbers

Exercise 5: Count the Number of Words in a String

Exercise 6: Reverse an Array

Exercise 7: Check for a Prime Number

Exercise 8: Remove Duplicates from an Array

Exercise 9: Calculate the Mean (Average) of an Array

Exercise 10: Check for Anagrams

Exercise 1: Calculate the Sum of an Array

Write a function sumArray that calculates the sum of all the numbers in an array.

function sumArray(arr) {

  let sum = 0;

  for (let num of arr) {

    sum += num;

  }

  return sum;

}

// Example usage:

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

Exercise 2: Find the Largest Element in an Array

Write a function findLargest that finds and returns the largest element in an array.

function findLargest(arr) {

  let largest = arr[0];

  for (let num of arr) {

    if (num > largest) {

      largest = num;

    }

  }

  return largest;

}

// Example usage:

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

Exercise 3: Calculate Factorial Using Recursion

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

function factorial(n) {

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

    return 1;

  }

  return n * factorial(n – 1);

}

// Example usage:

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

Exercise 4: Check for Even Numbers

Write a function isEven that checks if a given number is even and returns true if it is, and false otherwise.

function isEven(num) {

  return num % 2 === 0;

}

// Example usage:

console.log(isEven(8)); // Output: true

Exercise 5: Count the Number of Words in a String

Write a function countWords that counts the number of words in a given string.

function countWords(str) {

  const words = str.split(‘ ‘);

  return words.length;

}

// Example usage:

console.log(countWords(“This is a sample sentence.”)); // Output: 5

Exercise 6: Reverse an Array

Write a function reverseArray that reverses the elements in an array.

function reverseArray(arr) {

  return arr.reverse();

}

// Example usage:

console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]

Exercise 7: Check for a Prime Number

Write a function isPrime that checks if a given number is a prime number.

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;

}

// Example usage:

console.log(isPrime(11)); // Output: true

Exercise 8: Remove Duplicates from an Array

Write a function removeDuplicates that removes duplicate elements from an array.

function removeDuplicates(arr) {

  return […new Set(arr)];

}

// Example usage:

console.log(removeDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [1, 2, 3, 4, 5]

Exercise 9: Calculate the Mean (Average) of an Array

Write a function calculateMean that calculates the mean (average) of all the numbers in an array.

function calculateMean(arr) {

  const sum = arr.reduce((acc, num) => acc + num, 0);

  return sum / arr.length;

}

// Example usage:

console.log(calculateMean([1, 2, 3, 4, 5])); // Output: 3

Exercise 10: Check for Anagrams

Write a function areAnagrams that checks if two strings are anagrams of each other (they have the same characters but in different orders).

function areAnagrams(str1, str2) {

  const sortedStr1 = str1.split(”).sort().join(”);

  const sortedStr2 = str2.split(”).sort().join(”);

  return sortedStr1 === sortedStr2;

}

// Example usage:

console.log(areAnagrams(‘listen’, ‘silent’)); // Output: true

These exercises cover a variety of JavaScript concepts and can help you improve your coding skills. Try solving them to enhance your understanding of JavaScript programming.