Examples

Problems and Solutions

Array Sum

Write a function sumArray(arr) that accepts an array as an arg. The function should return the total sum of all values in the array. Solve this using Array#forEach.

For Each Solution

function sumArray(arr) {
  let total = 0;
  arr.forEach(function (num) {
    total += num;
  });
  return total;
}

Condensed For Each Solution

function sumArray(arr) {
  let total = 0;
  arr.forEach((num) => (total += num));
  return total;
}

Avg Val

Write a function avgVal(arr) that accepts an array as an arg. The function should return the average of all values in the array. If the array is empty, it should return null. Solve this using Array#forEach.

For Each Solution

Condensed For Each Solution


My Index Of Recall

Write a function myIndexOf(arr, target) that takes in an array and target value as args. The function should return the first index where the target is found in the array. If the target is not found, it should return -1. Solve this without using Array#indexOf.

For Loop Solution


Tripler

Write a function tripler(nums) that takes in an array as an arg. The function should return a new array containing three times every number of the original array. Solve this using Array#map.

Map Solution

Condensed Map Solution


Long Words

Write a function longWords(words) that takes in an array of words. The function should return an array containing only the words that are longer than 5 characters. Solve this using Array#filter.

Filter Solution

Condensed Filter Solution


Remove E Words

Write a function removeEWords(sentence) that accepts a sentence string as an arg. The function should return a new string, containing only the words that don't have the letter "e" in them. Solve this using Array#filter.

Filter Solution

Condensed Filter Solution


Max Value

Write a function maxValue(nums) that takes in an array of numbers as an arg. The function should return the largest number of the array. If the array is empty, the function should return null. You must use Array#forEach in your solution.

For Each Solution

Two Sum Recall

Write a function twoSum(arr, target) that accepts an array and a target number as args. The function should a return a boolean indicating if two distinct numbers of the array add up to the target value. You can assume that input array contains only unique numbers.

Double For Loop Solution


Two Dimensional Product

Write a function twoDimensionalProduct(array) that takes in a 2D array of numbers as an argument. The function should return the total product of all numbers multiplied together. Solve this using the Array#forEach method.

Chained forEach Solution

Flat Method Single For Each Solution

Condensed Flat Method Single For Each Solution

Harry's Chained Reduce Solution

Condensed Version of Above


Popper

Write a function popper(array, num) that takes in an array and a number as args. The function should remove the last num elements from the array, mutating the original array. The function should return a new array containing the elements that were removed.

Define this function using function expression syntax.

Straightforward Solution

For Loop Solution


Choose Primes

Write a function choosePrimes(nums) that takes in an array of numbers as args. The function should return a new array containing the primes from the original array. A prime number is a number that is only divisible by 1 and itself. Hint: consider creating a helper function to check if a number is prime! Helper Solution w/ Filter in Main

Helper Solution w/ forEach in Main


Fizz Buzz Recall

Write a function fizzBuzz(max) that accepts a number as an arg. The function should return an array containing all positive numbers less than max that are divisible by either 3 or 5, but not both.

Filter Solution

For Loop Solution


Longest Word

Write a function longestWord(sentence) that takes in a sentence string as an argument. The function should return the longest word in the sentence. You must use Array#forEach in your solution.

For Each Solution


Sum With Reduce

Write a function sumWithReduce(nums) that takes in an array of numbers. The function should return the total sum of all numbers in the array. Solve this using Array#reduce.

Condensed Reduce Solution

Reduce Solution


Abbreviate

Write a function abbreviate(word) that takes in a string arg. The function should return a new string where all of its vowels are removed.

Condensed Filter Solution

Filter Solution

For Loop Solution


Product With Reduce

Write a function productWithReduce(nums) that takes in an array of numbers. The function should return the total product of multiplying all numbers of the array together. You can assume that nums will not be an empty array. Solve this using Array#reduce.

Reduce Solution

Condensed Reduce Solution


Remove Last Vowel Recall

Write a function removeLastVowel(word) that takes in a string and returns the string with its last vowel removed. For Loop w/ Splice Solution

For Loop w/ Slice Solution


Abbreviate Words

Write a function abbreviateWords(sentence) that takes in a sentence string. The function should return a new sentence where words that are longer than 4 characters have their vowels removed. Hint: Consider creating a helper function to remove all vowels in a string.

Helper Solution w/ Map


Max With Reduce

Write a function maxWithReduce(nums) that takes in an array of numbers. The function should return the largest number of the array. You can assume that the array will not be empty. Solve this using Array#reduce.

Reduce Solution


Contains Word

Write a function containsWord(sentence, targetWord) that accepts two strings as args. The function should return a boolean indicating whether the targetWord is found inside of the sentence. Solve this without using #indexOf or #includes.

For Loop Solution


Uncompress

Write a function uncompress(str) that takes in a "compressed" string as an arg. A compressed string consists of a character immediately followed by the number of times it appears in the "uncompressed" form. The function should return a the uncompressed version of the string. See the examples.

Hint: you can use the built-in Number function should convert a numeric string into the number type. For example. Number("4") => 4

Double For Loop Solution

Single For Loop using Repeat Function


Hipsterfy

Write a function hipsterfy(sentence) that takes in a sentence string and returns the sentence where every word is missing it's last vowel.

Helper using Map

Helper using For Each


Least Common Multiple Recall

Write a function leastCommonMultiple(num1, num2) that accepts two numbers as arguments. The functions should return the smallest number that is divisible by both num1 and num2.

For Loop Solution

Harry's Recursive Solution single Function


Rotate

Write a function rotate(array, num) that takes in an array and a number as args. When the num is positive, the elements of the array should be rotated to the right. When the num is negative, the elements of the array should be rotated to the left. The function should mutate the original array.

Define this function using function expression syntax.

For Loop & Conditional Solution


Addition Mutator

Write a function additionMutator that accepts an array and a number as an arguments. The function should mutate the input array such that every element has the given number added to it.

For Loop Solution


Alternating Words

Write a function alternatingWords that accepts an array of words as an argument. The function should mutate the input array such that the words alternate between fully uppercase or lowercase. The first word should be uppercase.

For Loop Solution


Repeating Translate

Write a function repeatingTranslate that accepts a sentence as an argument. The function should translate the sentence according to the following rules:

  • words that are shorter than 3 characters are unchanged

  • words that are 3 characters or longer are translated according to the following rules:

    • if the word ends with a vowel, simply repeat the word twice (example: 'like'->'likelike')

    • if the word ends with a non-vowel, repeat all letters that come after the word's last vowel, including the last vowel itself (example: 'trash'->'trashash')

Note that if words are capitalized in the original sentence, they should remain capitalized in the translated sentence. Vowels are the letters a, e, i, o, u.

Single Helper Solution

Double Helper Solution


Last updated

Was this helpful?