Task’s
Basic Array Methods
1. push()
Easy: Write a function addToArray that adds a single number to the end of an
array.
Example: addToArray([1, 2], 3) → [1, 2, 3]
Medium: Write a function addMultipleToArray that adds multiple elements to
the end of an array.
Example: addMultipleToArray([1], 2, 3, 4) → [1, 2, 3, 4]
Hard: Write a function addUntilLimit that keeps pushing random numbers
between 1 and 10 to an array until its length reaches 10.
Example: [3, 7, 4, 6, ...] (Randomized array of length 10)
2. pop()
Easy: Write a function removeLast that removes the last element of an array.
Example: removeLast([1, 2, 3]) → [1, 2]
Task’s 1
Medium: Write a function removeUntil that keeps popping elements from the
array until its length becomes 2.
Example: removeUntil([1, 2, 3, 4, 5]) → [1, 2]
Hard: Write a function popMatching that removes elements from the end of an
array until an even number is found.
Example: popMatching([5, 7, 9, 10, 3]) → [5, 7, 9, 10]
3. shift()
Easy: Write a function removeFirst that removes the first element from an
array.
Example: removeFirst([1, 2, 3]) → [2, 3]
Medium: Write a function shiftWhileOdd that keeps removing the first
element of an array until the first element is even.
Example: shiftWhileOdd([1, 3, 5, 4, 6]) → [4, 6]
Hard: Write a function shiftAndLog that logs each removed element while
shifting elements off until the array is empty.
Example: Input: [1, 2]
Logs: 1 , 2
Output: []
4. unshift()
Easy: Write a function addAtStart that adds a number to the start of an array.
Example: addAtStart([2, 3], 1) → [1, 2, 3]
Medium: Write a function addMultipleAtStart that adds multiple numbers to
the start of an array.
Example: addMultipleAtStart([3], 1, 2) → [1, 2, 3]
Hard: Write a function addInReverse that adds numbers 1–10 to an empty
array using unshift() .
Example: [10, 9, 8, ..., 1]
5. splice()
Task’s 2
Easy: Write a function removeFromMiddle that removes 2 elements from an
array starting at index 1.
Example: removeFromMiddle([1, 2, 3, 4]) → [1, 4]
Medium: Write a function replaceMiddle that replaces 2 elements starting at
index 2 with 'A' and 'B' .
Example: replaceMiddle([1, 2, 3, 4]) → [1, 2, 'A', 'B']
Hard: Write a function insertSorted that inserts a number into a sorted array
at the correct position using splice() .
Example: insertSorted([1, 3, 5], 4) → [1, 3, 4, 5]
6. slice()
Easy: Write a function getFirstTwo that returns the first two elements of an
array.
Example: getFirstTwo([1, 2, 3]) → [1, 2]
Medium: Write a function getMiddle that returns elements from index 1 to 3
(inclusive).
Example: getMiddle([0, 1, 2, 3, 4]) → [1, 2, 3]
Hard: Write a function reverseWithoutChanging that returns a reversed array
without modifying the original array.
Example: reverseWithoutChanging([1, 2, 3]) → [3, 2, 1]
7. concat()
Easy: Write a function joinArrays that concatenates two arrays.
Example: joinArrays([1], [2, 3]) → [1, 2, 3]
Medium: Write a function concatMany that concatenates multiple arrays.
Example: concatMany([1], [2], [3]) → [1, 2, 3]
Hard: Write a function mergeAndSort that concatenates two arrays and sorts
the resulting array.
Example: mergeAndSort([3, 1], [4, 2]) → [1, 2, 3, 4]
Iterators and Search Methods
Task’s 3
8. every()
Easy: Write a function isAllPositive that checks if all elements in an array
are positive.
Example: isAllPositive([1, 2, 3]) → true
Medium: Write a function isAllStrings that checks if all elements in an array
are strings.
Example: isAllStrings(['a', 'b', 1]) → false
Hard: Write a function isAllEvenAfterTransform that checks if all numbers in an
array become even after doubling.
Example: isAllEvenAfterTransform([1, 2, 3]) → false
9. findIndex()
Easy: Write a function getIndexOfFirstEven that returns the index of the first
even number.
Example: getIndexOfFirstEven([1, 3, 4, 6]) → 2
Medium: Write a function findIndexByLength that returns the index of the first
string with a length greater than 5.
Example: findIndexByLength(['apple', 'pineapple']) → 1
Hard: Write a function getLastIndexMatching that finds the index of the last
number greater than 10.
Example: getLastIndexMatching([1, 20, 15, 9]) → 2
10. Advanced Combined Concepts
Hard: Write a function complexManipulation that:
1. Filters out negative numbers.
2. Sorts the array in ascending order.
3. Maps each number to its square.
4. Checks if all squared values are divisible by 4.
Example:
[-1, 2, -3, 4] → false
Task’s 4
1. map()
Easy: Write a function doubleNumbers that takes an array of numbers and
returns a new array where each number is doubled using map() .
Example: doubleNumbers([1, 2, 3]) → [2, 4, 6]
Medium: Write a function capitalizeWords that takes an array of words and
returns a new array where the first letter of each word is capitalized.
Example: capitalizeWords(['hello', 'world']) → ['Hello', 'World']
Hard: Write a function getDiscountedPrices that takes an array of objects
(each object has price and discount properties) and returns a new array
with the final discounted price for each object. Use conditional logic to
ensure discounts don’t exceed the price.
Example:
Input: [{price: 100, discount: 20}, {price: 50, discount: 60}]
Output: [80, 0]
2. filter()
Easy: Write a function getEvenNumbers that filters all even numbers from an
array.
Example: getEvenNumbers([1, 2, 3, 4]) → [2, 4]
Medium: Write a function filterAdults that filters out people under 18 from
an array of objects.
Example:
Input: [{name: 'Alice', age: 20}, {name: 'Bob', age: 16}]
Output: [{name: 'Alice', age: 20}]
Hard: Write a function getPrimeNumbers that filters prime numbers from an
array.
Example: getPrimeNumbers([2, 3, 4, 5, 6, 7]) → [2, 3, 5, 7]
(Hint: Write a helper function to check for primes.)
3. reduce()
Task’s 5
Easy: Write a function sumArray that returns the sum of all elements in an
array using reduce() .
Example: sumArray([1, 2, 3, 4]) → 10
Medium: Write a function countOccurrences that counts how many times each
element appears in an array.
Example: countOccurrences(['a', 'b', 'a']) → {a: 2, b: 1}
Hard: Write a function groupByCategory that groups an array of objects by a
property value (e.g., category ).
Example:
Input: [{item: 'apple', category: 'fruit'}, {item: 'carrot', category: 'vegetable'}]
Output: {fruit: [{item: 'apple'}], vegetable: [{item: 'carrot'}]}
4. forEach()
Easy: Write a function logElements that logs each element of an array to the
console using forEach() .
Example: logElements([1, 2, 3]) → 1 2 3 (logged to console)
Medium: Write a function createTable that takes an array of strings and
appends each string to a <ul> as a <li> element in HTML.
Example: createTable(['a', 'b']) → <ul><li>a</li><li>b</li></ul>
Hard: Write a function updateStock that modifies an array of objects in-place,
adding a new property inStock: true to all objects where quantity > 0 .
Example:
Input: [{name: 'apple', quantity: 3}, {name: 'banana', quantity: 0}]
Output: [{name: 'apple', quantity: 3, inStock: true}, {name: 'banana', quantity: 0}]
5. find()
Easy: Write a function findNumber that finds the first number greater than 10
in an array.
Example: findNumber([5, 15, 8]) → 15
Medium: Write a function findByName that finds an object by its name
property in an array of objects.
Task’s 6
Example:
Input: [{name: 'Alice'}, {name: 'Bob'}], name = 'Bob'
Output: {name: 'Bob'}
Hard: Write a function findPalindrome that finds the first palindrome string in
an array.
Example: findPalindrome(['apple', 'madam', 'banana']) → 'madam'
(Hint: Use a helper function to check for palindromes.)
6. some()
Easy: Write a function hasEvenNumber that checks if an array contains at least
one even number.
Example: hasEvenNumber([1, 2, 3]) → true
Medium: Write a function hasSubstring that checks if any string in an array
contains the substring 'car' .
Example: hasSubstring(['dog', 'cart']) → true
Hard: Write a function isCompleted that checks if any task in an array of
objects has a status of 'completed' .
Example:
Input: [{task: 'a', status: 'pending'}, {task: 'b', status: 'completed'}]
Output: true
7. Sorting ( sort() )
Easy: Write a function sortAscending that sorts an array of numbers in
ascending order.
Example: sortAscending([3, 1, 4]) → [1, 3, 4]
Medium: Write a function sortNames that sorts an array of strings
alphabetically, ignoring case.
Example: sortNames(['Bob', 'alice']) → ['alice', 'Bob']
Hard: Write a function sortByAge that sorts an array of objects by the age
property.
Example:
Task’s 7
Input: [{name: 'Alice', age: 25}, {name: 'Bob', age: 20}]
Output: [{name: 'Bob', age: 20}, {name: 'Alice', age: 25}]
8. Searching ( includes() , indexOf() )
Easy: Write a function containsNumber that checks if an array contains a
specific number.
Example: containsNumber([1, 2, 3], 2) → true
Medium: Write a function findIndexOfWord that returns the index of the first
occurrence of a word in an array.
Example: findIndexOfWord(['apple', 'banana'], 'banana') → 1
Hard: Write a function findNestedValue that checks if a specific value exists in
a nested array.
Example: findNestedValue([[1, 2], [3, 4]], 4) → true
9. Combination Challenge
Hard: Write a function analyzeArray that:
1. Filters out even numbers.
2. Doubles the remaining numbers.
3. Sums the resulting array.
Example:
Input:
[1, 2, 3, 4]
Output:
8 (Explanation: [1, 3] → [2, 6] → 8 )
Task’s 8