Skip to content

Instantly share code, notes, and snippets.

@Kalikoze
Last active September 12, 2018 17:25
Show Gist options
  • Save Kalikoze/8b0a50442f58f40df3cce9c45e2327e8 to your computer and use it in GitHub Desktop.
Save Kalikoze/8b0a50442f58f40df3cce9c45e2327e8 to your computer and use it in GitHub Desktop.

Missing Sheep

Consider an array of sheep where some sheep may be missing from their place. We need a function that returns the number of sheep present in the array (true means that this sheep is present).

var sheep = [
  true, true, true, false,
  true, true, true, true,
  true, false, true, false,
  true, false, false, true,
  true, true, true, true,
  false, false, true,  true
];

The correct answer would be 17.

Hello World

With an array that is mostly boolean, write a function that will return the first string.

For example, if we have the following array:

var things = [
  true, true, true, false,
  true, true, 1, true,
  true, false, true, false,
  true, 'hello', false, true,
  true, true, true, true,
  false, false, 'world', true
];

The correct answer would be hello.

The maths

Write a function called sum that takes an array of numbers and returns the sum of the numbers (which can include negative numbers). If the array does not contain any numbers then you should return 0.

Ex:

sum([]);

Our answer would be 0.

Ex:

var nums = [1, 5.2, 4, 0, -1];
sum(nums);

Our answer would be 9.2.

Lowercase words

Create a function called arrayLowerCase to lowercase all strings in an array. Non-string items should remain unchanged.

Ex:

var colors = ['Red', 'Green']
arrayLowerCase(colors)

Our answer would be ['red', 'green']

Ex:

var random = [1, 'Green'];

Our answer would be [1, 'green'].

Return an array that is in reverse alphabetical order

For example, if we have the following array:

var words = ['hi',  'hello', 'carrot', 'world'];

The correct answer would be ['world', 'hi', 'hello', 'carrot'].

Return the 3rd and 5th elements from an array in a new array

For example, if we have the following array:

var words = ['hi',  'hello', 'car', 'cool', 'blah'];

The correct answer would be ['car', 'blah'].

Return an array of crusts

For example, if we have the following array:

var pizzas = [
  { crust: 'deep dish' },
  { crust: 'pan' },
  { crust: 'thin' },
];

The correct answer would be ['deep dish', 'pan', 'thin'].

Merging Arrays

You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc 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. Also arr1 and arr2 may have same integers. Remove duplicated in the returned result.

arr1 = [1, 2, 3, 4, 5];
arr2 = [6, 7, 8, 9, 10];

Ex:

mergeArrays(arr1, arr2);

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

arr3 = [1, 3, 5, 7, 9];
arr4 = [10, 8, 6, 4, 2];

Ex:

mergeArrays(arr3, arr4);

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

Now we want to account for duplicate numbers:

arr5 = [1, 3, 5, 7, 9, 11, 12];
arr6 = [1, 2, 3, 4, 5, 10, 12];

Ex:

mergeArrays(arr5, arr6);

Our answer would be: [1, 2, 3, 4, 5, 7, 9, 10, 11, 12];

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