Skip to content

Instantly share code, notes, and snippets.

@Kalikoze
Last active May 28, 2019 16:50
Show Gist options
  • Save Kalikoze/99363e7c518f4fe72398e5ab30821fb0 to your computer and use it in GitHub Desktop.
Save Kalikoze/99363e7c518f4fe72398e5ab30821fb0 to your computer and use it in GitHub Desktop.

Adding, Looping, & Removing

You are given an array of people that live in a neighborhood. You should have two functions, one that is able to add families to the neighborhood and another that is able to remove them based on the number passed through.

Ex:

var neighborhood = ['Addams Family', 'The Jetsons', 'The Simpsons', 'Full House'];
addFamily(neighborhood, 'The Griffins');

Our answer would be: ['Addams Family', 'The Jetsons', 'The Simpsons', 'Full House', 'The Griffins']

Ex:

var neighborhood = ['Addams Family', 'The Jetsons', 'The Simpsons', 'Full House'];
removeFamilies(neighborhood, 2);

Our answer would be: ['The Simpsons', 'Full House'];

Merging Arrays

You are given two arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in descending order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays. You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return an empty array. Note: arr1 and arr2 may be sorted in different orders.

Ex:

var arr1 = [1, 2, 3, 4, 5];
var arr2 = [6, 7, 8, 9];
mergeArrays(arr1, arr2);

Our answer would be: [9, 8, 7, 6, 5, 4, 3, 2, 1];

Ex:

var arr3 = [1, 3, 5, 7, 9];
var arr4 = [8, 6, 4, 2];
mergeArrays(arr3, arr4);

Our answer would be: [9, 8, 7, 6, 5, 4, 3, 2, 1];

Finding and Removing

You have a to-do list and need to remove items from your list. Your task is to make a function that finds the to-do and removes it from the array. You might notice that the element does not always get removed from the beginning or end. How can we make this more dynamic?

Ex:

var errands = ['grocery shopping', 'vacuuming', 'laundry', 'meal prep', 'studying'];
removeFromList(errands, 'grocery shopping');

Our answer would be: ['vacuuming', 'laundry', 'meal prep', 'studying'];

Ex:

var healthList = ['eating vegetables', 'exercising', 'calling mom', 'resting enough'];
removeFromList(healthList, 'resting enough');

Our answer would be: ['eating vegetables', 'exercising', 'calling mom'];

Ex:

var funList = ['reading a book', 'playing video games', 'sleeping in', 'spending time with friends'];
removeFromList(funList, 'playing video games');

Our answer would be: ['reading a book', 'sleeping in', 'spending time with friends'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment