Mini Peaks

Edabit

Write a function that returns all the elements in an array that are strictly greater than their adjacent left and right neighbors.

Examples

miniPeaks([4, 5, 2, 1, 4, 9, 7, 2]) ➞ [5, 9]

miniPeaks([1, 2, 1, 1, 3, 2, 5, 4, 4]) ➞ [2, 3, 5]

miniPeaks([1, 2, 3, 4, 5, 6]) ➞ []

Notes

  • Do not count boundary numbers, since they only have one left/right neighbor.
  • If no such numbers exist, return an empty array.

 

Solution:

function miniPeaks(arr) {
  let sol = [];
  for (let i = 1; i < arr.length - 1; i++) {
    if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) { 
      sol.push(arr[i])
    }
  }
  return sol;
}

GCD of Two Numbers

Edabit

Write a function that returns the greatest common divisor (GCD) of two integers.

Examples

gcd(32, 8) ➞ 8

gcd(8, 12) ➞ 4

gcd(17, 13) ➞ 1

Notes

  • Both values will be positive.
  • The GCD is the largest factor that divides both numbers.

Solution:

function gcd(n1, n2) {
  let min = Math.min(n1, n2);
  while (min) {
    console.log(min);
    if (n1 % min === 0 && n2 % min === 0) {
      return min;
    }
    min--;
  }
}

Abbreviating a Sentence

Edabit

Create a function which takes a sentence and returns its abbreviation. Get all of the words over or equal to n characters in length and return the first letter of each, capitalised and overall returned as a single string.

Examples

abbreviate("do it yourself") ➞ "Y"

abbreviate("do it yourself", 2) ➞ "DIY"
// "do" and "it" are included because the second parameter specified that word lengths 2 are allowed.

abbreviate("attention AND deficit OR hyperactivity THE disorder")➞ "ADHD"
// Words below the default 4 characters are not included in the abbreviation.

abbreviate("the acronym of long word lengths", 5) ➞ "AL"
// "acronym" and "lengths" have 5 or more characters.

Notes

There may not be an argument given for n so set the default to 4.

Solution:

function abbreviate(...variable) {
  let len = variable.length === 1 ? 4 : variable[1];
  return variable[0]
    .split(" ")
    .filter(word => word.length >= len)
    .map(word => word[0])
    .join("")
    .toUpperCase();
}

Merge Two Arrays

Edabit

Create a function that takes two arrays and combines them by alternatingly taking elements from each array in turn.

  • The arrays may be of different lengths, with at least one character / digit.
  • The first array will contain string characters (lowercase, a-z).
  • The second array will contain integers (all positive).

Examples

mergeArrays(["f", "d", "w", "t"], [5, 3, 7, 8])
➞ ["f", 5, "d", 3, "w", 7, "t", 8]

mergeArrays([1, 2, 3], ["a", "b", "c", "d", "e", "f"])
➞ [1, "a", 2, "b", 3, "c", "d", "e", "f"]

mergeArrays(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5])
➞ ["a", 1, "b", 2, "c", 3, "d", 4, "e", 5]

Notes

N/A

Solution:

function mergeArrays(a, b) {
  const max = Math.max(a.length, b.length);
  let sol = [];
  for (let i = 0; i < max; i++) {
    sol.push(a[i]);
    sol.push(b[i]);
  }
  return sol.filter(elem => elem);
}

 

Return an Array of Subarrays

Edabit

Write a function that takes three arguments (x, y, z) and returns an array containing x subarrays (e.g. [[], [], []]), each containing y number of item z.

 

  • x Number of subarrays contained within the main array.
  • y Number of items contained within each subarray.
  • z Item contained within each subarray.

Examples

matrix(3, 2, 3) ➞ [[3, 3], [3, 3], [3, 3]]

matrix(2, 1, "edabit") ➞ [["edabit"], ["edabit"]]

matrix(3, 2, 0) ➞ [[0, 0], [0, 0], [0, 0]]

Notes

  • The first two arguments will always be integers.
  • The third argument is either a string or an integer.

Solution:

function matrix(x, y, z) {
  let sol = [];
  for (let i = 0; i < x; i++) {
    let temp = [];
    while (y--) {
      temp.push(z);
    }
    sol.push(temp);
  }
  return sol;
}

 

Letter Occurrences Per Word

Edabit

Create a function that takes in a sentence and a character to find. Return an object of each word in the sentence, with the count of the specified character as the value.

Examples

findOccurrences("Hello World", "o") ➞ {
  "hello" : 1,
  "world" : 1
}

findOccurrences("Create a nice JUICY function", "c") ➞  {
  "create" : 1,
  "a" : 0,
  "nice" : 1,
  "juicy" : 1,
  "function" : 1
}

findOccurrences("An APPLE a day keeps an Archeologist AWAY...", "A") ➞ {
  "an" : 1,
  "apple" : 1,
  "a" : 1,
  "day" : 1,
  "keeps" : 0,
  "archeologist" : 1,
  "away..." : 2
}

Notes

  • The function shouldn’t be case sensitive.
  • Words in the dictionary should be in lowercase.
  • You may be required to find punctuation.
  • Duplicate words should be ignored (see example #3 for the word “an”).

Solution:

function findOccurrences(str, char) {
  let sol = {};
  for (word of str.split(" ")) {
    let count = 0;
    for (ch of word.toLowerCase().split("")) {
      if (ch === char.toLowerCase()) count++;
    }
    sol[word.toLowerCase()] = count;
  }
  return sol;
}

 

Blah, Blah, Blah…

Create a function which replaces the last n words with "blah". Add "..." to the last blah.

Examples

blahBlah("A function is a block of code which only runs when it is called",  5) ➞ "A function is a block of code which only blah blah blah blah blah..."

blahBlah("one two three four five", 2) ➞ "one two three blah blah..."

blahBlah("Sphinx of black quartz judge my vow", 10) ➞ "blah blah blah blah blah blah blah..."

Notes

  • If n is longer than the amount of words in the sentence, replace every word with “blah” (see example #3).
  • All blahs shall be lowercase!

Solution:

function blahBlah(str, n) {
  let blh = "blah ".repeat(n).trim() + "...";
  let strArr = str.split(" ");
  let len = strArr.length;
  strArr = strArr.splice(0, len - n);

  return len < n
    ? "blah ".repeat(len).trim() + "..."
    : strArr.join(" ") + " " + blh;
}

 

Code After coffee 😛

function blahBlah(str, n) {
  let strArr = str.split(" ");
  return (
    strArr.map((word, i) => (i < strArr.length - n ? word : "blah")).join(" ") +
    "..."
  );
}

Edabit Experience Points

Edabit

As you complete questions on Edabit, you gain experience points depending on the difficulty of the question. The points for each difficulty are as follows:

Difficulty Experience Points
Very Easy 5XP
Easy 10XP
Medium 20XP
Hard 40XP
Very Hard 80XP

Given an object of how many questions a person has completed of each difficulty, return how many experience points they’ll have.

Examples

getXP({
  "Very Easy" : 89,
  "Easy" : 77,
  "Medium" : 30,
  "Hard" : 4,
  "Very Hard" : 1
}) ➞ "2055XP"


getXP({
  "Very Easy" : 254,
  "Easy" : 32,
  "Medium" : 65,
  "Hard" : 51,
  "Very Hard" : 34
}) ➞ "7650XP"


getXP({
  "Very Easy" : 11,
  "Easy" : 0,
  "Medium" : 2,
  "Hard" : 0,
  "Very Hard" : 27
}) ➞ "2255XP"

Notes

Return values as a string and make sure to add “XP” to the end.

Solution:

function getXP(obj) {
  return (
    obj["Very Easy"] * 5 +
    obj["Easy"] * 10 +
    obj["Medium"] * 30 +
    obj["Hard"] * 40 +
    obj["Very Hard"] * 80 +
    "XP"
  );
}

 

Algebra Sequence — Boxes

Create a function that takes a number (step) as an argument and returns the amount of boxes in that step of the sequence.

Box Sequence Image

  • Step 0: Start with 0
  • Step 1: Add 3
  • Step 2: Subtract 1
  • Repeat Step 1 & 2 …

Examples

boxSeq(0) ➞ 0

boxSeq(1) ➞ 3

boxSeq(2) ➞ 2

Notes

Step (the input) is always a positive integer (or zero).

Solution:

function boxSeq(step) {
  if (step === 0) {
    return 0;
  }
  let boxes = 3;
  for (let i = 2; i <= step; i++) {
    if (i % 2 === 0) {
      boxes -= 1;
    } else {
      boxes += 3;
    }
  }
  return boxes;
}

 

Python:

def box_seq(step):
	if step == 0:
		return 0;
	boxes = 3;
	for i in range(2,step+1):
		if i % 2 == 0:
			boxes -= 1;
		else:
			boxes += 3;
	return boxes;

3D Visual Block Sequence

Edabit

A block sequence in three dimensions. We can write a formula for this one:

Sequence Step 1 - 5

Create a function that takes a number (step) as an argument and returns the amount of blocks in that step.

Examples

blocks(1) ➞ 5

blocks(5) ➞ 39

blocks(2) ➞ 12

Notes

  • Step 0 obviously has to return 0.
  • The input is always a positive integer.
  • Check the Resources tab for a video on finding quadratic sequences.

 

Solution:

function booboo(n) {
  return n === 1 ? 1 : n + booboo(n - 1);
}

function blocks(step) {
  return step === 0 ? 0 : booboo(step) + step * 3 + step * 2 - 1;
}