Skip to content

Instantly share code, notes, and snippets.

@g-bel
g-bel / whoOwes.js
Created May 13, 2022 07:33
Cassidy Williams - question of the week 2021-05-09
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
@g-bel
g-bel / simpleAutocomplete.js
Created May 2, 2022 22:46
Cassidy Williams - question of the week 2021-05-02
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')));
@g-bel
g-bel / mergeIntervals.js
Created April 29, 2022 15:05
Cassidy Williams - question of the week 2021-04-25
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];
@g-bel
g-bel / largestSubarraySum.js
Created April 18, 2022 23:52
Cassidy Williams - question of the week 2021-04-18
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];
@g-bel
g-bel / interiorAngleSize.js
Created April 13, 2022 23:18
Cassidy Williams - question of the week 2021-04-11
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);
@g-bel
g-bel / equalWithDeletions.js
Created April 6, 2022 03:16
Cassidy Williams - question of the week 2021-04-04
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)) {
@g-bel
g-bel / containedItems.js
Created March 29, 2022 20:35
Cassidy Williams - question of the week 2021-03-27
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];
@g-bel
g-bel / smallestTimeInterval.js
Created March 24, 2022 17:38
Cassidy Williams question of the week 2021-03-21
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');