Sum of Prime Numbers [Edabit]

Create a function that takes an array of numbers and returns the sum of all prime numbers in the array.

Examples

sumPrimes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) ➞ 17

sumPrimes([2, 3, 4, 11, 20, 50, 71]) ➞ 87

sumPrimes([]) ➞ 0

Notes

  • Given numbers won’t exceed 101.
  • A prime number is a number which has exactly two divisors (1 and itself).

Solution:

function sumPrimes(arr) {
  let sol = 0;
  for (let num of arr) {
    if (isPrime(num)) {
      sol += num;
    }
  }
  return sol;
}

const isPrime = (num) => {
  if (num == 2 || num == 3) {
    return true;
  }
  if (num == 1) {
    return false;
  }
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
};

Changing a String into camelCase [Edabit]

Edabit

Using Camel Case (or camelCase) is where the first word is in lower case, and all words after it have their first letter capitalised. Note that there are no spaces in between words!

Create a function that takes a string and returns it back in camelCase.

Examples

camelCasing("Hello World") ➞ "helloWorld"

camelCasing("snake_case") ➞ "snakeCase"

camelCasing("low high_HIGH") ➞ "lowHighHigh"

Notes

  • You need to remove all spaces and underscores.
  • There will be no numbers in inputs.

Solution:

function camelCasing(str) {
  let strArr = str.replace(/[_]/g, " ").toLowerCase().split(" ");
  return strArr
    .map((w, index) => {
      if (index == 0) {
        return w;
      } else {
        return w[0].toUpperCase() + w.slice(1);
      }
    })
    .join("");
}

Case and Index Inverter [Edabit]

Edabit

Write a function that takes a string input and returns the string in a reversed case and order.

Examples

invert("dLROW YM sI HsEt") ➞ "TeSh iS my worlD"

invert("ytInIUgAsnOc") ➞ "CoNSaGuiNiTY"

invert("step on NO PETS") ➞ "step on NO PETS"

invert("XeLPMoC YTiReTXeD") ➞ "dExtErIty cOmplEx"

Notes

  • No empty strings and will neither contain special characters nor punctuation.
  • A recursive version of this challenge can be found via this link.

Solution:

function invert(s) {
  return s
    .split("")
    .reverse()
    .map((ch) => (ch.toUpperCase() == ch ? ch.toLowerCase() : ch.toUpperCase()))
    .join("");
}

Beginning and End Pairs [Edabit]

Edabit

Write a function that pairs the first number in an array with the last, the second number with the second to last, etc.

Examples

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

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

pairs([5, 9, 8, 1, 2]) ➞ [[5, 2], [9, 1], [8, 8]]

pairs([]) ➞ []

Notes

  • If the array has an odd length, repeat the middle element twice for the last pair.
  • Return an empty array if the input is an empty array.

Solution:

function pairs(arr) {
  let sol = [];
  while (arr.length) {
    if (arr.length == 1) {
      let num = arr.pop();
      sol.push([num, num]);
    } else {
      sol.push([arr.shift(), arr.pop()]);
    }
  }
  return sol;
}

Simple Counting [Edabit]

Edabit

Mubashir needs your help to count a specific digit in an array.

You have to create a function that takes two integers n and d and makes an array of squares of all numbers from 0…<= n and returns the count of the digits d in the array.

Examples

countDigits(10, 1) ➞ 4
// Squared array from 0 to 10 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
// Digit 1 appeared 4 times in the array

countDigits(25, 2) ➞ 9

countDigits(10, 1) ➞ 4

Notes

d will always be 0<=d<10.

Solution:

function countDigits(n, d) {
  let sol = 0;
  for (let i = 0; i <= n; i++) {
    let temp = i * i;
    if (temp == 0 && d == 0) {
      sol++;
    }
    while (temp) {
      if (d == temp % 10) {
        sol++;
      }
      temp = Math.floor(temp / 10);
    }
  }
  return sol;
}

Pronouncing the Xs [Edabit]

Edabit

Create a function that replaces all the x’s in the string in the following ways:

Replace all x’s with “cks” UNLESS:

  • The word begins with “x”, therefore replace it with “z”.
  • The word is just the letter “x”, therefore replace it with “ecks”.

Examples

xPronounce("Inside the box was a xylophone") ➞ "Inside the bocks was a zylophone"

xPronounce("The x ray is excellent") ➞ "The ecks ray is eckscellent"

xPronounce("OMG x box unboxing video x D") ➞ "OMG ecks bocks unbocksing video ecks D"

Notes

  • All x’s are lowercase.
  • I know that not all words with x’s follow this rule, but there are too many edge cases to count!

Solution:

function xPronounce(sentence) {
  let arr = sentence.split(" ").map((word) => {
    if (word.length == 1 && word == "x") {
      return "ecks";
    } else if (word[0] == "x") {
      word = "z" + word.slice(1);
    }
    return word.replace(/[x]/g, "cks");
  });
  return arr.join(" ");
}

A Simple Comparison [Edabit]

Edabit

Mubashir needs your help to compare two arrays.

First array arr1 contains some numbers and a second array arr2 contains squared values of numbers given in the first array.

Create a function that takes these two arrays and returns true if all square values are available, false otherwise.

arr1 = [121, 144, 19, 161, 19, 144, 19, 11]  
arr2 = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

Returns true because 121 is square of 11, 14641 is square of 121, 20736 is square of 144, 361 is square of 19, 25921 the square of 161, and so on…

arr1 = [121, 144, 19, 161, 19, 144, 19, 11] 
arr2 = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

Examples

simpleComp([121, 144, 19, 161, 19, 144, 19, 11], [121, 14641, 20736, 361, 25921, 361, 20736, 361]) ➞ true

simpleComp([4, 4], [1, 31]) ➞ false

simpleComp([2, 2, 3], [4, 4, 9]) ➞ true

Notes

Numbers can be in any order.

function simpleComp(arr1, arr2) {
  if (arr1 == null || arr2 == null) {
    return false;
  }
  if (arr1.length !== arr2.length) {
    return false;
  }
  arr1.sort();
  arr2.sort();
  let sol = arr1.filter((num, index) => arr2[index] === num * num);
  return sol.length === arr1.length;
}

Weekly Salary [Edabit]

Write a function that takes a list of hours and returns the total weekly salary.

  • The input list hours is listed sequentially, ordered from Monday to Sunday.
  • A worker earns $10 an hour for the first 8 hours.
  • For every overtime hour, he earns $15.
  • On weekends, the employer pays double the usual rate, regardless how many hours were worked previously that week. For instance, 10 hours worked on a weekday would pay 80+30 = $110, but on a weekend it would pay 160+60 = $220.

Examples

weeklySalary([8, 8, 8, 8, 8, 0, 0]) ➞ 400

weeklySalary([10, 10, 10, 0, 8, 0, 0]) ➞ 410

weeklySalary([0, 0, 0, 0, 0, 12, 0]) ➞ 280

Notes

Every element in the array is greater than or equal to 0.

function weeklySalary(hours) {
  let salary = 0;
  for (let i = 0; i < hours.length; i++) {
    if (i < 5) {
      if (hours[i] > 8) {
        salary += 8 * 10 + (hours[i] - 8) * 15;
      } else {
        salary += hours[i] * 10;
      }
    } else {
      if (hours[i] > 8) {
        salary += 8 * 20 + (hours[i] - 8) * 30;
      } else {
        salary += hours[i] * 20;
      }
    }
  }
  return salary;
}

Two Distinct Elements [Edabit]

Edabit

In each input array, every number repeats at least once, except for two. Write a function that returns the two unique numbers.

Examples

returnUnique([1, 9, 8, 8, 7, 6, 1, 6]) ➞ [9, 7]

returnUnique([5, 5, 2, 4, 4, 4, 9, 9, 9, 1]) ➞ [2, 1]

returnUnique([9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]) ➞ [5, 6]

Notes

Keep the same ordering in the output.

Solution:

function returnUnique(arr) {
  let sol = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr.lastIndexOf(arr[i]) === arr.indexOf(arr[i])) {
      sol.push(arr[i]);
    }
  }
  return sol;
}

Divide Array into Chunks [Edabit]

Edabit

Write a function that divides an array into chunks of size n, where n is the length of each chunk.

Examples

chunkify([2, 3, 4, 5], 2) ➞ [[2, 3], [4, 5]]

chunkify([2, 3, 4, 5, 6], 2) ➞ [[2, 3], [4, 5], [6]]

chunkify([2, 3, 4, 5, 6, 7], 3) ➞ [[2, 3, 4], [5, 6, 7]]

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

chunkify([2, 3, 4, 5, 6, 7], 7) ➞ [[2, 3, 4, 5, 6, 7]]

Notes

  • It’s O.K. if the last chunk is not completely filled (see example #2).
  • Integers will always be single-digit.

Solution:

function chunkify(arr, size) {
  let sol = [];
  while (arr.length) {
    let temp = [];
    let len = size;
    while (len--) {
      if (!arr.length) break;
      temp.push(arr.shift());
    }
    sol.push(temp);
  }
  return sol;
}
function chunkify(arr, size) {
  let sol = [];
  while (arr.length) {
    sol.push(arr.splice(0,size));
  }
  return sol;
}