Spicy Food

Edabit

The facts are:

  1. You’ve just finished dinner.
  2. You love spicy food but your friend hates it.

Given your friend’s unfortunate taste preferences, you decide to split the bill only for non-spicy items. You will pay in full for the spicy dishes.

Given two ordered arrays, one classifying the dishes as spicy vs. non-spicy and the other listing their prices, write a function that outputs an array where the first element is how much you pay and the second element is how much your friend pays.

billSplit(["S", "N", "S", "S"], [13, 18, 15, 4]) ➞ [41, 9]

// Since:
// You pay: [13, 9, 15, 4] = 41
// Friend pays: [0, 9, 0, 0] = 9

Examples

billSplit(["N", "S", "N"], [10, 10, 20]) ➞ [25, 15]
// You pay for half of both "N" dishes (5 + 10) and entirely pay for the "S" dish (10).

billSplit(["N", "N"], [10, 10]) ➞ [10, 10]

billSplit(["S", "N"], [41, 10]) ➞ [46, 5]

Notes

  • The dishes are in the same order for both input arrays.
  • Remember to output an array in this order: [your payment, friend's payment]
  • The array will contain at least one non-spicy dish N (you’re not going to let your poor friend go hungry, are you?).
  • Express both payments as integers.

Solution:

function billSplit(spicy, cost) {
  let me = 0, friend = 0;
  for (let i = 0; i < spicy.length; i++) {
    if (spicy[i] === "S") {
      me += cost[i];
    }
    else { 
      me += cost[i] / 2;
      friend += cost[i] / 2;
    }
  }
  return [me, friend];
}

 

Left Side, Right Side

Edabit

Create two functions:

  1. Leftside function: Returns count of numbers strictly smaller than n on the left.
  2. Rightside function: Returns count of numbers strictly smaller than n on the right.

Examples

leftSide([5, 2, 1, 4, 8, 7]) ➞ [0, 0, 0, 2, 4, 4]

rightSide([5, 2, 1, 4, 8, 7]) ➞ [3, 1, 0, 0, 1, 0]

leftSide([1, 2, 3, -1]) ➞ [0, 1, 2, 0]

rightSide([1, 2, 3, -1]) ➞ [1, 1, 1, 0]

Solution:

function leftSide(arr) {
  let sol = [];
  for (let i = 1; i <= arr.length; i++) {
    let sliceArr = arr.slice(0, i);
    if (sliceArr.length < 1) sol.push(0);
    else {
      let count = 0;
      for (let i = 0; i < sliceArr.length - 1; i++) {
        if (sliceArr[i] < sliceArr[sliceArr.length - 1]) count++;
      }
      sol.push(count);
    }
  }
  return sol;
}

function rightSide(arr) {
  let sol = [];
  newArr = arr.reverse();
  for (let i = 1; i <= newArr.length; i++) {
    let sliceArr = newArr.slice(0, i);
    if (sliceArr.length < 1) sol.push(0);
    else {
      let count = 0;
      for (let i = 0; i < sliceArr.length - 1; i++) {
        if (sliceArr[i] < sliceArr[sliceArr.length - 1]) count++;
      }
      sol.push(count);
    }
  }
  return sol.reverse();
}

 

Slidey Numbers

Edabit

A number can considered slidey if for every digit in the number, the next digit from that has an absolute difference of one. Check the examples below.

Examples

isSlidey(123454321) ➞ true

isSlidey(54345) ➞ true

isSlidey(987654321) ➞ true

isSlidey(1123) ➞ false

isSlidey(1357) ➞ false

Notes

  • A number cannot slide properly if there is a “flat surface” (example #4), or has gaps! (example #5).
  • All single digit numbers can be considered slidey numbers!

Solution:

function isSlidey(n) {
  if (Math.abs(n) < 10) return true;
  let prevNum = n % 10;
  n = Math.floor(n / 10);
  while (n > 0) {
    if (Math.abs(prevNum - (n % 10)) !== 1) {
      console.log(Math.abs(prevNum - (n % 10)));
      return false;
    }
    prevNum = n % 10;
    n = Math.floor(n / 10);
  }
  return true;
}

 

Neatly Formatted Math

Edabit

Given a simple math expression as a string, neatly format it as an equation.

Examples

formatMath("3 + 4") ➞ "3 + 4 = 7"

formatMath("3 - 2") ➞ "3 - 2 = 1"

formatMath("4 x 5") ➞ "4 x 5 = 20"

formatMath("6 / 3") ➞ "6 / 3 = 2"

Notes

  • You will need to deal with additionsubtractionmultiplication and division.
  • Division will have whole number answers (and will obviously not involve 0).

Solution:

function formatMath(expr) {
  return expr + " = " + eval(expr.replace("x", "*"));
}

 

Preventing the Collapse of the Universe

Dividing by 0 is a huge mistake and should be avoided at all costs.

Create a function that when given a math expression as a string, return True if at any point, the expression involves dividing by 0.

Examples

catchZeroDivision("2 / 0") ➞ true

catchZeroDivision("4 / (2 + 3 - 5)") ➞ true

catchZeroDivision("2 * 5 - 10") ➞ false

Notes

Multiplication signs will be given as an asterisk *.

Solution:

function catchZeroDivision(expr) {   return !isFinite(eval(expr)); }

 

Unique Character Mapping

Edabit

Write a function that returns a character mapping from a word.

Examples

characterMapping("abcd") ➞ [0, 1, 2, 3]

characterMapping("abb") ➞ [0, 1, 1]

characterMapping("babbcb") ➞ [0, 1, 0, 0, 2, 0]

characterMapping("hmmmmm") ➞ [0, 1, 1, 1, 1, 1]

Notes

  • Start your counter at 0, and increment by 1 each time you encounter a new character.
  • Identical characters should share the same number.

Solution:

function characterMapping(str) {
  let value = 0;
  let objMap = {};
  let sol = [];
  for (ch of [...str]) {
    if (objMap[ch] === undefined) {
      if (value === 0) {
        sol.push(value);
        objMap[ch] = value;
        value++;
      } else {
        if (value === 0) {
          value++;
          objMap[ch] = value;
        } else {
          objMap[ch] = value;
          value++;
        }
        sol.push(objMap[ch]);
      }
    } else {
      sol.push(objMap[ch]);
    }
  }
  return sol;
}

 

The Array Twins

Edabit

Create a function that given an array, it returns the index where if splited in two-subarrays (last element of the first array has index of (foundIndex-1)), the sum of them are equal.

Examples

twins([10, 20, 30, 5, 40, 50, 40, 15]) ➞ 5
// foundIndex 5 : [10+20+30+5+40]=[50+40+15]

twins([1, 2, 3, 4, 5, 5]) ➞ 4
// [1, 2, 3, 4] [5, 5]

twins([3, 3]) ➞ 1

Notes

Return only the foundIndex, not the divided arrays!

Solution:

function twins(arr) {
  for (let i = 1; i < arr.length; i++) {
    if (
      arr.slice(0, i).reduce((acc, curr) => acc + curr) ===
      arr.slice(i).reduce((acc, curr) => acc + curr)
    ) {
      return i;
    }
  }
  return -1;
}

 

Phone Number Word Decoder

Edabit

Create a program that converts a phone number with letters to one with only numbers.

Number Letter
0 none
1 none
2 ABC
3 DEF
4 GHI
5 JKL
6 MNO
7 PQRS
8 TUV
9 WXYZ

Examples

textToNum("123-647-EYES") ➞ "123-647-3937"

textToNum("(325)444-TEST") ➞ "(325)444-8378"

textToNum("653-TRY-THIS") ➞ "653-879-8447"

textToNum("435-224-7613") ➞ "435-224-7613"

Notes

  • All inputs will be formatted as a string representing a proper phone number in the format XXX-XXX-XXXX or (XXX)XXX-XXXX, using numbers and capital letters.
  • Check the Resources tab for more info on telephone keypads.

Solution:

function textToNum(phone) {
  let obj = {
    A: 2,
    B: 2,
    C: 2,
    D: 3,
    E: 3,
    F: 3,
    G: 4,
    H: 4,
    I: 4,
    J: 5,
    K: 5,
    L: 5,
    M: 6,
    N: 6,
    O: 6,
    P: 7,
    Q: 7,
    R: 7,
    S: 7,
    T: 8,
    U: 8,
    V: 8,
    W: 9,
    X: 9,
    Y: 9,
    Z: 9
  };
  let sol = "";
  for (ch of [...phone]) {
    if (obj[ch]) {
      sol += obj[ch];
    } else {
      sol += ch;
    }
  }
  return sol;
}

 

Moving to the End

Edabit

Write a function that moves all elements of one type to the end of the array.

Examples

moveToEnd([1, 3, 2, 4, 4, 1], 1) ➞ [3, 2, 4, 4, 1, 1]
// Move all the 1s to the end of the array.

moveToEnd([7, 8, 9, 1, 2, 3, 4], 9) ➞ [7, 8, 1, 2, 3, 4, 9]

moveToEnd(["a", "a", "a", "b"], "a") ➞ ["b", "a", "a", "a"]

Notes

Keep the order of the un-moved items the same.

Solution:

function moveToEnd(arr, el) {
  let withoutEL = arr.filter(num => num !== el);
  let withEL = arr.filter(num => num === el);
  return withoutEL.concat(withEL);
}