Code Challenges
Solve problems, earn XP, and level up your skills.
Hello World Function
Write a function called `greet` that takes a name as a parameter and returns the string "Hello, [name]!". Example: greet('Alice') → "Hello, Alice!" greet('World') → "Hello, World!"
Sum of Two Numbers
Write a function `add(a, b)` that returns the sum of two numbers. Example: add(2, 3) → 5 add(-1, 1) → 0 add(0, 0) → 0
Even or Odd
Write a function `isEven(n)` that returns `true` if the number is even, or `false` if it is odd. Example: isEven(4) → true isEven(7) → false isEven(0) → true
Reverse a String
Write a function `reverseString(str)` that returns the string reversed. Example: reverseString('hello') → 'olleh' reverseString('world') → 'dlrow' reverseString('abcd') → 'dcba'
Factorial
Write a function `factorial(n)` that returns the factorial of a non-negative integer. Factorial is the product of all positive integers up to n. Example: factorial(5) → 120 (5 × 4 × 3 × 2 × 1) factorial(0) → 1 factorial(3) → 6
Find Maximum in Array
Write a function `findMax(arr)` that returns the largest number in an array. Example: findMax([1, 3, 2]) → 3 findMax([10, 5, 20]) → 20 findMax([-1, -5, -3]) → -1
Count Vowels
Write a function `countVowels(str)` that returns the number of vowels (a, e, i, o, u) in a string (case-insensitive). Example: countVowels('hello') → 2 countVowels('JavaScript') → 3 countVowels('rhythm') → 0
Palindrome Check
Write a function `isPalindrome(str)` that returns `true` if the string reads the same forwards and backwards (ignore case), `false` otherwise. Example: isPalindrome('racecar') → true isPalindrome('hello') → false isPalindrome('Madam') → true
FizzBuzz
Write a function `fizzBuzz(n)` that returns an array of strings for numbers 1 to n: - "Fizz" for multiples of 3 - "Buzz" for multiples of 5 - "FizzBuzz" for multiples of both - The number as a string otherwise Example: fizzBuzz(5) → ["1", "2", "Fizz", "4", "Buzz"]
Remove Duplicates
Write a function `removeDuplicates(arr)` that returns a new array with all duplicate values removed, keeping the first occurrence. Example: removeDuplicates([1,2,2,3,3,4]) → [1,2,3,4] removeDuplicates(['a','b','a','c']) → ['a','b','c']
Fibonacci Sequence
Write a function `fibonacci(n)` that returns the nth Fibonacci number (0-indexed). The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, ... Example: fibonacci(0) → 0 fibonacci(1) → 1 fibonacci(6) → 8 fibonacci(10) → 55
Flatten Array
Write a function `flatten(arr)` that takes a nested array and returns a flat (1D) array. Example: flatten([1, [2, 3], [4, [5]]]) → [1, 2, 3, 4, 5] flatten([[1, 2], [3, [4, [5]]]]) → [1, 2, 3, 4, 5]
Anagram Check
Write a function `isAnagram(str1, str2)` that returns `true` if the two strings are anagrams of each other (same letters, different order), ignoring case and spaces. Example: isAnagram('listen', 'silent') → true isAnagram('hello', 'world') → false isAnagram('Astronomer', 'Moon starer') → true
Chunk Array
Write a function `chunk(arr, size)` that splits an array into chunks of a given size. Example: chunk([1,2,3,4,5], 2) → [[1,2],[3,4],[5]] chunk([1,2,3,4,5,6], 3) → [[1,2,3],[4,5,6]]
Deep Clone Object
Write a function `deepClone(obj)` that returns a deep copy of an object (nested objects should also be cloned, not referenced). Example: const a = { x: 1, y: { z: 2 } }; const b = deepClone(a); b.y.z = 99; // a.y.z should still be 2
Group By Property
Write a function `groupBy(arr, key)` that groups an array of objects by a given key. Example: groupBy([{type:'a',val:1},{type:'b',val:2},{type:'a',val:3}], 'type') → { a: [{type:'a',val:1},{type:'a',val:3}], b: [{type:'b',val:2}] }
Debounce Function
Write a function `debounce(fn, delay)` that returns a debounced version of `fn` — the function only executes after `delay` milliseconds have passed without it being called again. This is a classic interview question. Describe how debounce works and implement it. Hint: Use `setTimeout` and `clearTimeout`.
Memoize Function
Write a function `memoize(fn)` that returns a memoized version of `fn`. A memoized function caches its results — if called again with the same arguments, it returns the cached result instead of recomputing. Example: const slow = (n) => { /* expensive */ return n * 2; }; const fast = memoize(slow); fast(5); // computes → 10 fast(5); // returns cached → 10 (no recompute)
Implement Promise.all
Implement a function `myPromiseAll(promises)` that mimics the behavior of `Promise.all`: - Resolves with an array of results when all promises resolve - Rejects immediately if any promise rejects Do NOT use `Promise.all` in your implementation.
Binary Search
Write a function `binarySearch(arr, target)` that searches a sorted array for `target` and returns its index, or -1 if not found. You must use the binary search algorithm (O(log n)), not linear search. Example: binarySearch([1,3,5,7,9], 5) → 2 binarySearch([1,3,5,7,9], 4) → -1 binarySearch([2,4,6,8,10], 10) → 4