Sharpen Your JavaScript Skills with These Coding Exercises

Exercise 1: Reverse a String

Write a function reverseString that takes a string as input and returns the reverse of that string.

function reverseString(str) {

  return str.split(”).reverse().join(”);

}

// Example usage:

console.log(reverseString(‘Hello’)); // Output: “olleH”

Exercise 2: 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 3: Find the Maximum Number

Write a function findMax that takes an array of numbers as input and returns the maximum number in the array.

function findMax(numbers) {

  return Math.max(…numbers);

}

// Example usage:

console.log(findMax([3, 7, 1, 9, 4])); // Output: 9

Exercise 4: FizzBuzz

Write a function fizzBuzz that prints numbers from 1 to n, but for multiples of 3, print “Fizz” instead, and for multiples of 5, print “Buzz” instead. For numbers that are multiples of both 3 and 5, print “FizzBuzz.”

function fizzBuzz(n) {

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

    if (i % 3 === 0 && i % 5 === 0) {

      console.log(‘FizzBuzz’);

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

      console.log(‘Fizz’);

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

      console.log(‘Buzz’);

    } else {

      console.log(i);

    }

  }

}

// Example usage:

fizzBuzz(15);

Exercise 5: 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 6: Check for Prime Number

Write a function isPrime that checks if a given number is prime (only divisible by 1 and itself).

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 7: Find Fibonacci Sequence

Write a function generateFibonacci that generates the Fibonacci sequence up to a specified number of terms.

function generateFibonacci(n) {

  const fibArray = [0, 1];

  while (fibArray.length < n) {

    const nextValue = fibArray[fibArray.length – 1] + fibArray[fibArray.length – 2];

    fibArray.push(nextValue);

  }

  return fibArray;

}

// Example usage:

console.log(generateFibonacci(10)); // Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Exercise 8: Find Longest Word

Write a function findLongestWord that takes a string of words and returns the longest word in the string.

function findLongestWord(str) {

  const words = str.split(‘ ‘);

  let longestWord = ”;

  for (const word of words) {

    if (word.length > longestWord.length) {

      longestWord = word;

    }

  }

  return longestWord;

}

// Example usage:

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

Exercise 9: Count Characters

Write a function countCharacters that counts the occurrences of each character in a string and returns the results as an object.

function countCharacters(str) {

  const charCount = {};

  for (const char of str) {

    if (charCount[char]) {

      charCount[char]++;

    } else {

      charCount[char] = 1;

    }

  }

  return charCount;

}

// Example usage:

console.log(countCharacters(‘hello’)); // Output: {h: 1, e: 1, l: 2, o: 1}

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 cover a range of JavaScript concepts and are designed to help you practice your coding skills and problem-solving abilities.