JavaScript Cheat Sheet Challenges

JavaScript Cheat Sheet Challenges

Boost your JavaScript knowledge with our JavaScript Cheat Sheet Challenges. From basic concepts to advanced topics, these challenges are designed to enhance your understanding and application of JavaScript in real-world scenarios.

Table of Contents

Easy

1. Challenge: Write a JavaScript program to print “Hello, World!” to the console.

Answer:

console.log("Hello, World!");

2. Challenge: Write a JavaScript function to find the sum of two numbers.

Answer:

function sum(a, b) {
    return a + b;
}

console.log(sum(5, 10));

3. Challenge: Write a JavaScript program to check if a number is even or odd.

Answer:

const number = 7;
if (number % 2 === 0) {
    console.log("The number is even.");
} else {
    console.log("The number is odd.");
}

4. Challenge: Write a JavaScript function to reverse a string.

Answer:

function reverseString(str) {
    return str.split('').reverse().join('');
}

console.log(reverseString("Hello, World!"));

5. Challenge: Write a JavaScript function to find the maximum of three numbers.

Answer:

function findMax(a, b, c) {
    return Math.max(a, b, c);
}

console.log(findMax(3, 6, 2));

Medium

6. Challenge: Write a JavaScript function to count the number of vowels in a given string.

Answer:

function countVowels(str) {
    const vowels = 'aeiouAEIOU';
    let count = 0;
    for (let char of str) {
        if (vowels.includes(char)) {
            count++;
        }
    }
    return count;
}

console.log(countVowels("Hello World"));

7. Challenge: Write a JavaScript function to find the factorial of a number using recursion.

Answer:

function factorial(n) {
    if (n === 0 || n === 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

console.log(factorial(5));

8. Challenge: Write a JavaScript function to check if a string is a palindrome.

Answer:

function isPalindrome(str) {
    return str === str.split('').reverse().join('');
}

console.log(isPalindrome("racecar"));

9. Challenge: Write a JavaScript function to find the Greatest Common Divisor (GCD) of two numbers.

Answer:

function gcd(a, b) {
    while (b) {
        [a, b] = [b, a % b];
    }
    return a;
}

console.log(gcd(48, 18));

10. Challenge: Write a JavaScript function to find the second largest element in an array.

Answer:

function findSecondLargest(arr) {
    const uniqueNumbers = [...new Set(arr)];
    uniqueNumbers.sort((a, b) => b - a);
    return uniqueNumbers[1];
}

console.log(findSecondLargest([1, 3, 4, 4, 5, 6, 6, 7]));

Hard

11. Challenge: Write a JavaScript function to implement the Fibonacci sequence using dynamic programming.

Answer:

function fibonacci(n) {
    const fib = [0, 1];
    for (let i = 2; i <= n; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }
    return fib[n];
}

console.log(fibonacci(10));

12. Challenge: Write a JavaScript function to find all prime numbers up to a given number using the Sieve of Eratosthenes.

Answer:

function sieveOfEratosthenes(n) {
    const primes = Array(n + 1).fill(true);
    primes[0] = primes[1] = false;
    for (let p = 2; p * p <= n; p++) {
        if (primes[p]) {
            for (let i = p * p; i <= n; i += p) {
                primes[i] = false;
            }
        }
    }
    return primes.map((isPrime, index) => isPrime ? index : null).filter(Boolean);
}

console.log(sieveOfEratosthenes(30));

13. Challenge: Write a JavaScript function to implement a binary search algorithm.

Answer:

function binarySearch(arr, x) {
    let low = 0, high = arr.length - 1;
    while (low <= high) {
        const mid = Math.floor((low + high) / 2);
        if (arr[mid] === x) {
            return mid;
        } else if (arr[mid] < x) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

console.log(binarySearch([1, 2, 3, 4, 5, 6, 7], 4));

14. Challenge: Write a JavaScript function to solve the “N-Queens” problem using backtracking.

Answer:

function isSafe(board, row, col, n) {
    for (let i = 0; i < col; i++) {
        if (board[row][i]) return false;
    }
    for (let i = row, j = col; i >= 0 && j >= 0; i--, j--) {
        if (board[i][j]) return false;
    }
    for (let i = row, j = col; j >= 0 && i < n; i++, j--) {
        if (board[i][j]) return false;
    }
    return true;
}

function solveNQueens(board, col, n) {
    if (col >= n) return true;
    for (let i = 0; i < n; i++) {
        if (isSafe(board, i, col, n)) {
            board[i][col] = 1;
            if (solveNQueens(board, col + 1, n)) return true;
            board[i][col] = 0;
        }
    }
    return false;
}

const n = 4;
const board = Array.from({ length: n }, () => Array(n).fill(0));
solveNQueens(board, 0, n);
console.log(board);

10. Challenge: Write a JavaScript function to perform Merge Sort.

Answer:

function mergeSort(arr) {
    if (arr.length <= 1) return arr;

    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid));
    const right = mergeSort(arr.slice(mid));

    return merge(left, right);
}

function merge(left, right) {
    let result = [], l = 0, r = 0;
    while (l < left.length && r < right.length) {
        if (left[l] < right[r]) {
            result.push(left[l]);
            l++;
        } else {
            result.push(right[r]);
            r++;
        }
    }
    return result.concat(left.slice(l)).concat(right.slice(r));
}

const array = [12, 11, 13, 5, 6, 7];
console.log("Sorted array:", mergeSort(array));

Are you interested in more coding challenges? Check out additional challenges on our website to enhance your skills and knowledge.