Find Max in ArrayProblem StatementWrite a function `findMax(arr)` that takes an array of numbers and returns the largest number.Approach 1. Initialize a `max` variable with the first element. 2. Iterate through the array. 3. If current element > `max`, update `max`. 4. Return `max`. Solution`function findMax(arr) { if (arr.length === 0) return undefined; let max = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i];
Reverse a StringProblem StatementWrite a function `reverseString(str)` that returns the string in reverse.Approach 1. Convert the string to an array. 2. Reverse the array. 3. Join the array back into a string. Solution`function reverseString(str) { return str.split('').reverse().join('');
Palindrome CheckerProblem StatementWrite a function `isPalindrome(str)` that returns `true` if a string is a palindrome.Approach 1. Reverse the string. 2. Compare the reversed string with the original. Solution`function isPalindrome(str) { const reversed = str.split('').reverse().join(''); return str === reversed;
FizzBuzzProblem StatementWrite a function `fizzBuzz(n)` that returns an array of numbers from 1 to n, but replace: - Multiples of 3 with "Fizz" - Multiples of 5 with "Buzz" - Multiples of both with "FizzBuzz"Approach 1. Create an empty array. 2. Loop from 1 to n. 3. Use if/else if to check conditions (start with both 3 and 5). 4. Push the result to the array. Solution`function fizzBuzz(n) { const result = []; for (let i = 1; i <= n; i++) { if (i % 15 === 0) result.push("FizzBuzz"); else if (i % 3 === 0) result.push("Fizz"); else if (i % 5 === 0) result.push("Buzz"); else result.push(i);
Find FactorialProblem StatementWrite a function `factorial(n)` that returns the factorial of n.Approach 1. If n is 0 or 1, return 1. 2. Otherwise, use a loop or recursion to multiply numbers down to 1. Solution`function factorial(n) { if (n === 0 || n === 1) return 1; let res = 1; for (let i = 2; i <= n; i++) { res *= i;
Sum of ArrayProblem StatementWrite a function `sumArray(arr)` that returns the sum of all numbers in an array.Approach 1. Initialize a `sum` variable to 0. 2. Loop through the array and add each element to `sum`. Solution`function sumArray(arr) { return arr.reduce((acc, curr) => acc + curr, 0);
Count VowelsProblem StatementWrite a function `countVowels(str)` that counts the number of vowels in a string.Approach 1. Define a string/array of vowels. 2. Loop through the input string. 3. If the character is a vowel, increment count. Solution`function countVowels(str) { const vowels = 'aeiouAEIOU'; let count = 0; for (let char of str) { if (vowels.includes(char)) count++;
Even or OddProblem StatementWrite a function `isEven(n)` that returns `true` if n is even, `false` otherwise.Approach 1. Use the modulo operator `%`. 2. A number is even if `n % 2 === 0`. Solution`function isEven(n) { return n % 2 === 0;
Leap Year CheckerProblem StatementWrite a function `isLeapYear(year)` that returns `true` if a year is a leap year.Approach 1. Divisible by 4 is mostly a leap year. 2. Except if divisible by 100, then it must also be divisible by 400. Solution`function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
Multiplication TableProblem StatementWrite a function `multiTable(n, limit)` that returns an array strings like `"n x i = result"` up to the limit.Approach 1. Loop from 1 to the limit. 2. Use template literals to format the string. Solution`function multiTable(n, limit) { const table = []; for (let i = 1; i <= limit; i++) { table.push(`${n
Smallest NumberProblem StatementWrite a function `findMin(arr)` that returns the smallest number in an array.Approach 1. Initialize `min` with the first element. 2. Loop through and update `min` if a smaller value is found. Solution`function findMin(arr) { if (arr.length === 0) return null; return Math.min(...arr);
Filter Positive NumbersProblem StatementWrite a function `filterPositive(arr)` that returns an array with only positive numbers.Approach 1. Use the `.filter()` method. 2. The condition is `num > 0`. Solution`function filterPositive(arr) { return arr.filter(num => num > 0);
Celsius to FahrenheitProblem StatementWrite a function `celsiusToFahrenheit(c)` that converts Celsius to Fahrenheit.Approach 1. Formula: `(C * 9/5) + 32`. Solution`function celsiusToFahrenheit(c) { return (c * 9/5) + 32;
Fibonacci SequenceProblem StatementWrite a function `fibonacci(n)` that returns the first n numbers of the Fibonacci sequence.Approach 1. Handle edge cases for n = 0, 1, 2. 2. Initialize array with [0, 1]. 3. Loop from 2 to n-1, adding previous two numbers. Solution`function fibonacci(n) { if (n <= 0) return []; if (n === 1) return [0]; const seq = [0, 1]; while (seq.length < n) { seq.push(seq[seq.length - 1] + seq[seq.length - 2]);
Check Prime NumberProblem StatementWrite a function `isPrime(n)` that returns `true` if a number is prime.Approach 1. Return false if n <= 1. 2. Loop from 2 up to the square root of n. 3. If n is divisible by any, return false. Solution`function isPrime(n) { if (n <= 1) return false; for (let i = 2; i <= Math.sqrt(n); i++) { if (n % i === 0) return false;
Title Case a SentenceProblem StatementWrite a function `titleCase(str)` that capitalizes the first letter of each word.Approach 1. Split string by spaces into an array of words. 2. Map through words, capitalize index 0 and add rest of word as lowercase. 3. Join back with spaces. Solution`function titleCase(str) { return str.split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(' ');
Remove DuplicatesProblem StatementWrite a function `removeDuplicates(arr)` that returns a new array with unique elements.Approach 1. Use the `Set` object for easy uniqueness. 2. Convert back to array using Spread operator. Solution`function removeDuplicates(arr) { return [...new Set(arr)];
Sum of DigitsProblem StatementWrite a function `sumDigits(n)` that returns the sum of all digits in a number.Approach 1. Convert number to positive string. 2. Split into characters, map back to numbers, and reduce to sum. Solution`function sumDigits(n) { return Math.abs(n).toString().split('').reduce((acc, d) => acc + Number(d), 0);
Count Character OccurrencesProblem StatementWrite a function `countChar(str, char)` that counts how many times `char` appears in `str`.Approach 1. Initialize count to 0. 2. Loop through characters of string. 3. If character matches target, increment count. Solution`function countChar(str, char) { let count = 0; for (let c of str) { if (c === char) count++;
Check if SortedProblem StatementWrite a function `isSorted(arr)` that returns `true` if an array is sorted in ascending order.Approach 1. Loop from index 0 to length - 2. 2. If any element is greater than the next one, return false. Solution`function isSorted(arr) { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) return false;