Going through edabit stage by stage to see my power level.
Uh... never mind, the very easy one's are a little...easy. Let's move on to...
A decimal number can be represented as a sequence of bits. To illustrate:
6 = 00000110
23 = 00010111
From the bitwise representation of numbers, we can calculate the bitwise AND, bitwise OR and bitwise XOR. Using the example above:
bitwiseAND(6, 23) ➞ 00000110
bitwiseOR(6, 23) ➞ 00010111
bitwiseXOR(6, 23) ➞ 00010001
Write three functions to calculate the bitwise AND, bitwise OR and bitwise XOR of two numbers.
bitwiseAND(7, 12) ➞ 4
bitwiseOR(7, 12) ➞ 15
bitwiseXOR(7, 12) ➞ 11
JavaScript has a useful function: toString(2)
, where you can see the binary representation of a decimal number.
bitwiseAND
bitwiseOR
bitwiseXOR
// Bonus
toString
toNum
Create a function that takes two arrays and insert the second array in the middle of the first array.
tuckIn([1, 10], [2, 3, 4, 5, 6, 7, 8, 9]) ➞ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
tuckIn([15,150], [45, 75, 35]) ➞ [15, 45, 75, 35, 150]
tuckIn([[1, 2], [5, 6]], [[3, 4]]) ➞ [[1, 2], [3, 4], [5, 6]]
tuckIn
You will be given an array of drinks, with each drink being an object with two properties: name and price. Create a function that has the drinks array as an argument and return the drinks objects sorted by price in ascending order.
Assume that the following array of drink objects needs to be sorted:
drinks = [
{name: "lemonade", price: 50},
{name: "lime", price: 10}
]
The output of the sorted drinks object will be:
sortDrinkByPrice(drinks) ➞ [{name: "lime", price: 10}, {name: "lemonade", price: 50}]
N/A
let drinks = [
]
sortDrinkByPrice
A word is on the loose and now has tried to hide amongst a crowd of tall letters! Help write a function to detect what the word is, knowing the following rules:
detectWord("UcUNFYGaFYFYGtNUH") ➞ "cat"
detectWord("bEEFGBuFBRrHgUHlNFYaYr") ➞ "burglar"
detectWord("YFemHUFBbezFBYzFBYLleGBYEFGBMENTment") ➞ "embezzlement"
N/A
hideWord
detectWord
Create a method in the Person
class which returns how another person's age compares. Given the instances p1
, p2
and p3
, which will be initialised with the attributes name
and age
, return a sentence in the following format:
{other person name} is {older than / younger than / the same age as} me.
p1 = Person("Samuel", 24)
p2 = Person("Joel", 36)
p3 = Person("Lily", 24)
p1.compareAge(p2) ➞ "Joel is older than me."
p2.compareAge(p1) ➞ "Samuel is younger than me."
p1.compareAge(p3) ➞ "Lily is the same age as me."
let [p1,p2] = [
]
p1.compareAge
Write a function that takes a two-digit number and determines if it's the largest of two possible digit swaps.
To illustrate:
largestSwap(27) ➞ false
largestSwap(43) ➞ true
If 27 is our input, we should return false
because swapping the digits gives us 72, and 72 > 27. On the other hand, swapping 43 gives us 34, and 43 > 34.
largestSwap(14) ➞ false
largestSwap(53) ➞ true
largestSwap(99) ➞ true
Numbers with two identical digits (third example) should yield true
(you can't do better).
largestSwap
Okay, I'm getting bored. On to...
Create a function which returns the number of true
values there are in an array.
countTrue([true, false, false, true, false]) ➞ 2
countTrue([false, false, false, false]) ➞ 0
countTrue([]) ➞ 0
0
if given an empty array.true
or false
).const arr = [
]
countTrue
Create a function that will return an integer number corresponding to the amount of digits in the given integer num
.
num_of_digits(1000) ➞ 4
num_of_digits(12) ➞ 2
num_of_digits(1305981031) ➞ 10
num_of_digits(0) ➞ 1
Try to solve this challenge without using strings!
num_of_digits
Write a function that takes a number and returns the perimeter of either a circle or a square. The input will be in the form (letter l
, number num
) where the letter will be either "s"
for square, or "c"
for circle, and the number will be the side of the square or the radius of the circle.
Use the following formulas:
Perimeter of a square: 4 * side.
Perimeter of a circle: 6.28 * radius.
The catch is you can only use arithmetic or comparison operators, which means:
The goal is to write a short, branch-free piece of code.
perimeter("s", 7) ➞ 28
perimeter("c", 4) ➞ 25.12
perimeter("c", 9) ➞ 56.52
No rounding is needed.
perimeter
perimeter
Try finding your ancestors and offspring with code.
Create a function that takes a number x
and a character y
("m"
for male, "f"
for female), and returns the name of an ancestor (m/f) or descendant (m/f).
0
. In the case of 0
(male or female), return "me!"
.generation(2, "f") ➞ "granddaughter"
generation(-3, "m") ➞ "great grandfather"
generation(1, "f") ➞ "daughter"
Check the Resources tab for helpful hints.
Generation | Male | Female |
---|---|---|
-3 | great grandfather | great grandmother |
-2 | grandfather | grandmother |
-1 | father | mother |
0 | me! | me! |
1 | son | daughter |
2 | grandson | granddaughter |
3 | great grandson | great granddaughter |
generation
Write a function redundant
that takes in a string str
and returns a function that returns str
.
const f1 = redundant("apple")
f1() ➞ "apple"
const f2 = redundant("pear")
f2() ➞ "pear"
const f3 = redundant("")
f3() ➞ ""
Your function should return a function, not a string.
const fn = redundant;
fn();
In a board game, a piece may advance 1-6 tiles forward depending on the number rolled on a six-sided die. If you advance your piece onto the same tile as another player's piece, both of you earn a bonus.
Can you reach your friend's tile number in the next roll? Create a function that takes your position a
and your friend's position b
and returns a boolean representation of whether it's possible to earn a bonus on any die roll.
possibleBonus(3, 7) ➞ true
possibleBonus(1, 9) ➞ false
possibleBonus(5, 3) ➞ false
false
, as you would be advancing away.possibleBonus
A tetrahedron is a pyramid with a triangular base and three sides. A tetrahedral number is a number of items within a tetrahedron.
Create a function that takes an integer n
and returns the n
th tetrahedral number.
tetra(2) ➞ 4
tetra(5) ➞ 35
tetra(6) ➞ 56
There is a formula for the n
th tetrahedral number.
tetra
betterTetra
Ooh.
Create a function that returns the sum of missing numbers from the given array.
sumMissingNumbers([4, 3, 8, 1, 2]) ➞ 18
// 5 + 6 + 7 = 18
sumMissingNumbers([17, 16, 15, 10, 11, 12]) ➞ 27
// 13 + 14 = 27
sumMissingNumbers([1, 2, 3, 4, 5]) ➞ 0
// No Missing Numbers (i.e. all numbers in [1, 5] are present in the list)
The numerical range to consider when searching for the missing numbers in the array is the sequence of consecutive numbers between the minimum and maximum of the numbers (inclusive).
sumMissingNumbers
A boomerang is a V-shaped sequence that is either upright or upside down. Specifically, a boomerang can be defined as: sub-array of length 3, with the first and last digits being the same and the middle digit being different.
Some boomerang examples:
[3, 7, 3], [1, -1, 1], [5, 6, 5]
Create a function that returns the total number of boomerangs in an array.
To illustrate:
[3, 7, 3, 2, 1, 5, 1, 2, 2, -2, 2]
// 3 boomerangs in this sequence: [3, 7, 3], [1, 5, 1], [2, -2, 2]
Be aware that boomerangs can overlap, like so:
[1, 7, 1, 7, 1, 7, 1]
// 5 boomerangs (from left to right): [1, 7, 1], [7, 1, 7], [1, 7, 1], [7, 1, 7], and [1, 7, 1]
countBoomerangs([9, 5, 9, 5, 1, 1, 1]) ➞ 2
countBoomerangs([5, 6, 6, 7, 6, 3, 9]) ➞ 1
countBoomerangs([4, 4, 4, 9, 9, 9, 9]) ➞ 0
[5, 5, 5]
(triple identical digits) is NOT considered a boomerang because the middle digit is identical to the first and last.
let arr = [
]
countBoomerangs
There are three towers. The objective of the game is to move all the disks over to tower #3, but you can't place a larger disk onto a smaller disk. To play the game or learn more about the Tower of Hanoi, check the Resources tab.
Create a function that takes a number discs
as an argument and returns the minimum amount of steps needed to complete the game.
towerHanoi(3) ➞ 7
towerHanoi(5) ➞ 31
towerHanoi(0) ➞ 0
towerHanoi
Write a function that counts how many concentric layers a rug has.
countLayers([
"AAAA",
"ABBA",
"AAAA"
]) ➞ 2
countLayers([
"AAAAAAAAA",
"ABBBBBBBA",
"ABBAAABBA",
"ABBBBBBBA",
"AAAAAAAAA"
]) ➞ 3
countLayers([
"AAAAAAAAAAA",
"AABBBBBBBAA",
"AABCCCCCBAA",
"AABCAAACBAA",
"AABCADACBAA",
"AABCAAACBAA",
"AABCCCCCBAA",
"AABBBBBBBAA",
"AAAAAAAAAAA"
]) ➞ 5
const rug1 = [
]
countLayers;
const rug2 = [
]
countLayers;
You can think of character classes as characters with special meaning. They are recognized as special when you place the \
before the character.
Here are a list of the characters classes in JavaScript:
., \cX, \d, \D, \f, \n, \r, \s, \S, \t, \v, \w, \W, \0, \xhh, \uhhhh, \uhhhhh, [\b]
HTML elements are everything from the start tag to the end tag. An example of one div
element would be: <div>edabit</div>
.
Find out how many <div>
elements are used in a string. Use the character class \W
in your expression.
const str = "<div>Hello.</div><div>My name is <b>George</b>.</div>"
// 2 times
const str = "<div><h1>The Word for Today</h1><div>aardvark</div></div>"
// 2 times
const str = "<div></div>"
// 1 time
Check the Resources tab for details on character classes if you're stuck.
const str1 =
;
pakTestExp
const str2 =
;
pakTestExp
const str3 =
;
pakTestExp
An array is positive dominant if it contains strictly more unique positive values than unique negative values. Write a function that returns true
if an array is positive dominant.
isPositiveDominant([1, 1, 1, 1, -3, -4]) ➞ false
// There is only 1 unique positive value (1).
// There are 2 unique negative values (-3, -4).
isPositiveDominant([5, 99, 832, -3, -4]) ➞ true
isPositiveDominant([5, 0]) ➞ true
isPositiveDominant([0, -4, -1]) ➞ false
0
counts as neither a positive nor a negative value.
isPositiveDominant;
isPositiveDominant;
Create a function that takes an array of numbers and return "Boom!"
if the digit 7 appears in the array. Otherwise, return "there is no 7 in the array"
.
sevenBoom([1, 2, 3, 4, 5, 6, 7]) ➞ "Boom!"
// 7 contains the number seven.
sevenBoom([8, 6, 33, 100]) ➞ "there is no 7 in the array"
// None of the items contain 7 within them.
sevenBoom([2, 55, 60, 97, 86]) ➞ "Boom!"
// 97 contains the number seven.
N/A
sevenBoom
simpleSevenBoom
// Bonus!
getDigits
Create a function that takes two dates and returns the number of days between the first and second date.
getDays(
new Date("June 14, 2019"),
new Date("June 20, 2019")
) ➞ 6
getDays(
new Date("December 29, 2018"),
new Date("January 1, 2019")
) ➞ 3
// Dates may not all be in the same month/year.
getDays(
new Date("July 20, 2019"),
new Date("July 30, 2019")
) ➞ 10
N/A
const [date1,date2] = [
]
getDays
Create a function that determines whether a number is Oddish or Evenish. A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even. If a number is Oddish, return "Oddish"
. Otherwise, return "Evenish"
.
For example, oddishOrEvenish(121)
should return "Evenish"
, since 1 + 2 + 1 = 4. oddishOrEvenish(41)
should return "Oddish"
, since 4 + 1 = 5.
oddishOrEvenish(43) ➞ "Oddish"
// 4 + 3 = 7
// 7 % 2 = 1
oddishOrEvenish(373) ➞ "Oddish"
// 3 + 7 + 3 = 13
// 13 % 2 = 1
oddishOrEvenish(4433) ➞ "Evenish"
// 4 + 4 + 3 + 3 = 14
// 14 % 2 = 0
N/A
oddishOrEvenish
Kay, let's just move on.
Write a function that moves all the zeroes to the end of an array. Do this without returning a copy of the input array.
zeroesToEnd([1, 2, 0, 0, 4, 0, 5]) ➞ [1, 2, 4, 5, 0, 0, 0]
zeroesToEnd([0, 0, 2, 0, 5]) ➞ [2, 5, 0, 0, 0]
zeroesToEnd([4, 4, 5]) ➞ [4, 4, 5]
zeroesToEnd([0, 0]) ➞ [0, 0]
zeroesToEnd
Create a function that takes a name of a chemical compound as a string and returns the molar mass of the compound. For this challenge only, required data is included below.
H -> 1
B -> 10
O -> 16
S -> 32
N -> 14
Cl -> 35
Water = "H2 O"
BoricAcid = "H3 B O3"
SulfuricAcid = "H2 S O4"
NitricAcid = "H N O3"
HydroChloricAcid = "H Cl"
molarMass("SulfuricAcid") -> "H2 S O4"
#H * 2 + S * 1 + O * 4 -----> 1 * 2 + 32 * 1 + 16 * 4 = 98
molarMass("Water") -> "H2 O"
#H * 2 + O * 1 -----> 1 * 2 + 16 * 1 = 18
molarMass
Write a recursive function that takes a string input and returns the string in a reversed case and order.
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"
invert
recursiveInvert
In the game Set, three cards form a set if each of the cards are identical or completely different for each of the four properties. All three cards must:
The four properties are:
Here, a set is represented by an array containing three cards. Each card is represented by an object of properties and values. Write a function that determines whether three cards constitute a valid set.
Here is an example of a set:
[
{ color: "red", number: 1, shade: "empty", shape: "squiggle" },
{ color: "red", number: 2, shade: "lined", shape: "diamond" },
{ color: "red", number: 3, shade: "full", shape: "oval" }
]
// "Same" properties: color
// "Different" properties: numbers, shading and shapes
The following is not a set:
[
{ color: "red", number: 1, shade: "empty", shape: "squiggle" },
{ color: "red", number: 2, shade: "lined", shape: "diamond" },
{ color: "purple", number: 3, shade: "full", shape: "oval" }
]
// Colors are not all identical, but not all different.
isSet([
{ color: "green", number: 1, shade: "empty", shape: "squiggle" },
{ color: "green", number: 2, shade: "empty", shape: "diamond" },
{ color: "green", number: 3, shade: "empty", shape: "oval" }
]) ➞ true
isSet([
{ color: "purple", number: 1, shade: "full", shape: "oval" },
{ color: "green", number: 1, shade: "full", shape: "oval" },
{ color: "red", number: 1, shade: "full", shape: "oval" }
]) ➞ true
isSet([
{ color: "purple", number: 3, shade: "full", shape: "oval" },
{ color: "green", number: 1, shade: "full", shape: "oval" },
{ color: "red", number: 3, shade: "full", shape: "oval" }
]) ➞ false
const cards = [
]
isSet;
Given a grid of numbers, return a grid of the Spotlight Sum of each number. The spotlight sum can be defined as the total of all numbers immediately surrounding the number on the grid, including the number in the total.
spotlightMap([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]) ➞ [
[12, 21, 16],
[27, 45, 33],
[24, 39, 28]
]
spotlightMap([
[2, 6, 1, 3, 7],
[8, 5, 9, 4, 0]
]) ➞ [
[21, 31, 28, 24, 14],
[21, 31, 28, 24, 14]
]
spotlightMap([[3]]) ➞ [[3]]
const grid = [
]
spotlightMap;