This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const whoOwes = (receipts) => { | |
const people = {}; | |
let total = 0; | |
for (const { name, paid } of receipts) { | |
people[name] = people[name] === undefined ? paid : people[name] + paid; | |
total += paid; | |
} | |
const totalEach = Math.floor(total * 100 / Object.keys(people).length) / 100; // Divide with cents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict = ['apple', 'banana', 'cranberry', 'strawberry']; | |
const simpleAutocomplete = (text) => dict.filter(str => str.includes(text)); | |
console.log(JSON.stringify(simpleAutocomplete('app'))); | |
console.log(JSON.stringify(simpleAutocomplete('berry'))); | |
console.log(JSON.stringify(simpleAutocomplete('fart'))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeIntervals = (arr) => { | |
let result = [arr[0]]; | |
let resultIndex = 0; | |
for (let i = 1; i < arr.length; i++) { | |
if (result[resultIndex][1] < arr[i][0]) { | |
resultIndex++; | |
result.push(arr[i]); | |
} else { | |
if (result[resultIndex][1] < arr[i][1]) { | |
result[resultIndex][1] = arr[i][1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const largestSubarraySum = (arr, arrLength) => { | |
let highestSubArr = arr.splice(0, arrLength); | |
let tempSubArr = [...highestSubArr]; | |
let total = highestSubArr.reduce((t, c) => t += c, 0); | |
let tempSubArrTotal = total; | |
for (let i = 0; i < arr.length; i++) { | |
let tmp; | |
[tmp, ...tempSubArr] = [...tempSubArr, arr[i]]; | |
tempSubArrTotal = tempSubArrTotal - tmp + arr[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const interiorAngleSize = (amoutnOfSides) => amoutnOfSides < 3 ? "N/A" : | |
(180 * (amoutnOfSides - 2)) / amoutnOfSides; | |
const area = (amoutnOfSides, sideLenght) => amoutnOfSides < 3 || sideLenght <= 0 ? "N/A" : | |
(amoutnOfSides / 4) * (1 / Math.tan(Math.PI / amoutnOfSides)); | |
const inscribedCircleRadius = (amoutnOfSides, sideLenght) => amoutnOfSides < 3 || sideLenght <= 0 ? "N/A" : | |
(sideLenght / 2) * (1 / Math.tan(Math.PI / amoutnOfSides)); | |
const circumscribedCircleRadius = (amoutnOfSides, sideLenght) => amoutnOfSides < 3 || sideLenght <= 0 ? "N/A" : | |
(sideLenght / 2) * (1 / Math.sin(Math.PI / amoutnOfSides)); | |
const roundDecimals = (num, decimals) => | |
Number(num).toFixed(decimals); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const equalWithDeletions = (strOne, strTwo) => { | |
console.log('Result: ', cleanStr(strOne) === cleanStr(strTwo)); | |
}; | |
const cleanStr = (str) => { | |
let text = str; | |
while (text.includes('#') || text.includes('%')) { | |
const hash = text.indexOf('#'); | |
const amp = text.lastIndexOf('%'); | |
if (hash >= 0 && ((amp >= 0 && hash < amp) || amp < 0)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const containedItems = (str, pairArray) => { | |
// The inner Array contains the result, the partial value and whether it started counting | |
const results = Array(pairArray.length).fill([0, 0, false]); | |
let boxOpenIndex = -1; | |
for (let i = 0; i < str.length; i++) { | |
boxOpenIndex = str[i] === '|' ? i : boxOpenIndex; | |
for (let x = 0; x < pairArray.length; x++) { | |
const [start, end] = pairArray[x]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const smallestTimeInterval = (times) => { | |
let minTime = 24 * 60; | |
for (let i = 0; i < times.length -1; i++) { | |
const thisInterval = stringTimeToMins(times[i].split(':')); | |
const nextInterval = stringTimeToMins(times[i+1].split(':')); | |
const intervalDifference = nextInterval - thisInterval; | |
minTime = intervalDifference < minTime && intervalDifference > 0 ? intervalDifference : minTime; | |
} | |
if(minTime < 60) { | |
return printTime(minTime, 'minute'); |