15 November 2024

Lesson 4

JavaScript Practice Tasks

Here are 15 more JavaScript tasks to help you practice and strengthen your understanding. These tasks range from basic to intermediate.


Task 1: Check Prime Number

Write a function isPrime that takes a number as input and returns true if the number is prime, otherwise false. Prime numbers are numbers that are only divisible by 1 and themselves.

Example:

isPrime(7); // Output: true
isPrime(10); // Output: false

Task 2: Reverse a String

Write a function reverseString that takes a string and returns it reversed.

Example:

reverseString("hello"); // Output: "olleh"

Task 3: Count Vowels

Write a function countVowels that takes a string and returns the number of vowels in the string.

Example:

countVowels("javascript"); // Output: 3

Task 4: Remove Duplicates from Array

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

Example:

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

Task 5: Find Missing Number

Given an array of numbers from 1 to N with one number missing, write a function findMissingNumber to find the missing number.

Example:

findMissingNumber([1, 2, 4, 5]); // Output: 3

Task 6: Palindrome Number

Write a function isPalindromeNumber that checks if a number reads the same backward as forward.

Example:

isPalindromeNumber(121); // Output: true
isPalindromeNumber(123); // Output: false

Task 7: Flatten a Nested Array

Write a function flattenArray that takes a nested array and returns a flattened version of it.

Example:

flattenArray([1, [2, [3, 4]], 5]); // Output: [1, 2, 3, 4, 5]

Task 8: Sort an Array

Write a function sortArray that takes an array of numbers and returns it sorted in ascending order.

Example:

sortArray([5, 3, 8, 1]); // Output: [1, 3, 5, 8]

Task 9: FizzBuzz with Custom Range

Modify the FizzBuzz problem to take two numbers, start and end, and perform FizzBuzz for that range.

Example:

fizzBuzzRange(10, 15);
// Output:
// Buzz
// 11
// Fizz
// 13
// 14
// FizzBuzz

Task 10: Find Intersection of Two Arrays

Write a function arrayIntersection that returns the intersection of two arrays (common elements).

Example:

arrayIntersection([1, 2, 3], [2, 3, 4]); // Output: [2, 3]

Task 11: Fibonacci Sequence

Write a function fibonacci that generates the first n numbers in the Fibonacci sequence.

Example:

fibonacci(5); // Output: [0, 1, 1, 2, 3]

Task 12: Find Factorial (Iterative)

Write a function factorialIterative to calculate the factorial of a number using a loop.

Example:

factorialIterative(5); // Output: 120

Task 13: Check Anagram

Write a function isAnagram that checks if two strings are anagrams of each other.

Example:

isAnagram("listen", "silent"); // Output: true
isAnagram("hello", "world");   // Output: false

Task 14: Sum of Digits

Write a function sumOfDigits that takes a number and returns the sum of its digits.

Example:

sumOfDigits(123); // Output: 6

Task 15: Count Occurrences of a Character

Write a function countCharacter that counts how many times a given character appears in a string.

Example:

countCharacter("hello world", "o"); // Output: 2