Fun with JavaScript Quick Coding Exercises to practice and test your skills with JavaScript Code

Learn to Code JavaScript

Find the largest number in an array:
Check if a string is a palindrome:
Calculate the sum of an array:
Check if a number is even or odd:
Reverse a string:
Find the factorial of a number:
Write a function to check if a given number is even or odd.
Write a function to reverse a string.
Write a function to find the largest number in an array.
Write a function to find the sum of all numbers in an array.
Write a function to remove all duplicates from an array.
Write a function to check if a given string is a palindrome.
Write a function to find the factorial of a given number.
Write a function to find the nth Fibonacci number.
Write a function to sort an array of numbers in ascending order.
Write a function to find the second largest number in an array.
Write a function that takes in a string and returns the reverse of that string.
Write a function that takes in an array of numbers and returns the sum of all the numbers.
Write a function that takes in an array of strings and returns the longest string.
Write a function that takes in a number and returns true if it is even, false if it is odd.

Find the largest number in an array:

const arr = [10, 5, 20, 35, 15];

const largestNum = Math.max(…arr);

console.log(largestNum); // Output: 35

Explanation: The Math.max() method is used to find the largest number in an array. The spread operator … is used to pass the array elements as individual arguments to the Math.max() method.

Check if a string is a palindrome:

const isPalindrome = str => {

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

  return str === reversedStr;

};

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

console.log(isPalindrome(‘hello’)); // Output: false

Explanation: The isPalindrome() function takes a string as input and checks if it’s the same when reversed. The split() method is used to convert the string into an array, the reverse() method is used to reverse the array, and the join() method is used to convert the reversed array back into a string.

Calculate the sum of an array:

const arr = [10, 5, 20, 35, 15];

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

console.log(sum); // Output: 85

Explanation: The reduce() method is used to iterate over the array and accumulate the sum of all elements. The acc parameter is the accumulator, which starts at 0, and curr is the current element being iterated over.

Check if a number is even or odd:

const isEven = num => num % 2 === 0;

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

console.log(isEven(5)); // Output: false

Explanation: The isEven() function takes a number as input and checks if it’s even or odd by using the modulo operator %. If the result of num % 2 is 0, the number is even.

Reverse a string:

const reverseStr = str => str.split(”).reverse().join(”);

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

Explanation: The reverseStr() function takes a string as input and returns the reverse of the string by using the same methods as in the palindrome example.

Find the factorial of a number:

const factorial = num => {

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

    return 1;

  } else {

    return num * factorial(num – 1);

  }

};

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

Explanation: The factorial() function takes a number as input and calculates its factorial recursively by multiplying the number with the factorial of the number minus one. The base case is when the input number is 0 or 1, in which case the function returns 1.

Write a function to check if a given number is even or odd.

function isEven(num) {

  return num % 2 === 0;

}

This function takes in a number as an argument and checks if it’s even by using the modulo operator to see if the remainder is 0. If it’s even, the function returns true, otherwise it returns false.

Write a function to reverse a string.

function reverseString(str) {

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

}

This function takes in a string as an argument, splits it into an array of characters, reverses the order of the array, and joins it back into a string.

Write a function to find the largest number in an array.

function findLargest(arr) {

  return Math.max(…arr);

}

This function takes in an array of numbers as an argument and uses the Math.max method to find the largest number in the array.

Write a function to find the sum of all numbers in an array.

function findSum(arr) {

  return arr.reduce((acc, curr) => acc + curr, 0);

}

This function takes in an array of numbers as an argument and uses the reduce method to add up all the numbers in the array and return the sum.

Write a function to remove all duplicates from an array.

function removeDuplicates(arr) {

  return […new Set(arr)];

}

This function takes in an array as an argument and uses the Set object to remove all duplicates, then converts it back to an array using the spread operator.

Write a function to check if a given string is a palindrome.

function isPalindrome(str) {

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

  return str === reversed;

}

This function takes in a string as an argument, reverses it using the reverse method, and checks if it’s equal to the original string.

Write a function to find the factorial of a given number.

function factorial(num) {

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

    return 1;

  } else {

    return num * factorial(num – 1);

  }

}

This function uses recursion to calculate the factorial of a given number. If the number is 0 or 1, it returns 1. Otherwise, it multiplies the number by the factorial of the number minus 1.

Write a function to find the nth Fibonacci number.

function fibonacci(n) {

  if (n <= 1) {

    return n;

  } else {

    return fibonacci(n – 1) + fibonacci(n – 2);

  }

}

This function uses recursion to find the nth Fibonacci number. If n is 0 or 1, it returns n. Otherwise, it returns the sum of the previous two Fibonacci numbers.

Write a function to sort an array of numbers in ascending order.

function ascendingSort(arr) {

  return arr.sort((a, b) => a – b);

}

This function takes in an array of numbers as an argument and uses the sort method to sort them in ascending order.

Write a function to find the second largest number in an array.

function secondLargest(arr) {

  const sorted = arr.sort((a, b) => b – a);

  return sorted[1];

}

This function takes in an array of numbers as an argument and sorts them in descending order using the

Write a function that takes in a string and returns the reverse of that string.

function reverseString(str) {

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

}

console.log(reverseString(“hello”)); // Output: “olleh”

console.log(reverseString(“world”)); // Output: “dlrow”

Explanation:

This function takes in a string, splits it into an array of characters, reverses the order of the array using the reverse() method, and then joins the characters back into a string.

Write a function that takes in an array of numbers and returns the sum of all the numbers.

function sumArray(arr) {

  let sum = 0;

  for (let i = 0; i < arr.length; i++) {

    sum += arr[i];

  }

  return sum;

}

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

console.log(sumArray([5, 10, 15])); // Output: 30

Explanation:

This function takes in an array of numbers, initializes a variable sum to 0, and then loops through each element in the array, adding each number to the sum variable. Finally, it returns the sum.

Write a function that takes in an array of strings and returns the longest string.

function longestString(arr) {

  let longest = “”;

  for (let i = 0; i < arr.length; i++) {

    if (arr[i].length > longest.length) {

      longest = arr[i];

    }

  }

  return longest;

}

console.log(longestString([“hello”, “world”, “foo”, “bar”])); // Output: “hello”

console.log(longestString([“apple”, “banana”, “pear”, “kiwi”])); // Output: “banana”

Explanation:

This function takes in an array of strings, initializes a variable longest to an empty string, and then loops through each element in the array. If the length of the current string is greater than the length of longest, it sets longest to the current string. Finally, it returns longest.

Write a function that takes in a number and returns true if it is even, false if it is odd.

function isEven(num) {

  if (num % 2 === 0) {

    return true;

  } else {

    return false;

  }

}

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

console.log(isEven(3)); // Output: false

Explanation:

This function takes in a number and uses the modulo operator (%) to determine whether it is even or odd. If the remainder when the number is divided by 2 is 0, the number is even and the function returns true. Otherwise, it returns false.