Last active
July 18, 2020 05:17
-
-
Save exbotanical/e406fd207527aa4cc5824e67d027e904 to your computer and use it in GitHub Desktop.
more codegolf
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 rowWeights = arr => { | |
let a = 0; | |
let b = 0; | |
Object.entries(arr).forEach(([i, val]) => { | |
i % 2 === 0 ? a += val : b += val; | |
}); | |
return [a, b]; | |
} | |
/* | |
Prompt | |
Given an array of positive integers (the weights of the people), return a new array/tuple of two integers, | |
where the first one is the total weight of team 1, and the second one is the total weight of team 2. | |
*/ |
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 type = obj => Object.prototype.toString.apply(obj).replace(/\[object (.+)\]/i, "$1").toLowerCase(); |
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 humanYearsCatYearsDogYears = n => n === 1 ? [1, 15, 15] : ((n) => { | |
let v = 0; | |
for (let i = 0; i <= n; i++) { | |
i === 1 ? v += 15 : i === 2 ? v += 9 : i > 2 ? v += 4 : null; | |
}; | |
return [n, v, v + (n - 2)]; | |
})(n); | |
/* | |
Code Golfin' | |
Prompt | |
"Return their respective ages now as [humanYears,catYears,dogYears] | |
NOTES: | |
humanYears >= 1 | |
humanYears are whole numbers only | |
Cat Years | |
15 cat years for first year | |
+9 cat years for second year | |
+4 cat years for each year after that | |
Dog Years | |
15 dog years for first year | |
+9 dog years for second year | |
+5 dog years for each year after that" | |
*/ |
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 validateCode = (code, matches) => matches.includes(Number((code+"")[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 digitsAverage = input => { | |
if (input < 10) { | |
return input; | |
} | |
while (input > 9) { | |
input = String(input); | |
let arr = []; | |
for (let i = 0; i < input.length - 1; i++) { | |
arr.push(Math.round((+input[i] + +input[i + 1]) / 2)); | |
} | |
input = parseInt(arr.join("")); | |
if (input < 10) { | |
return input; | |
} | |
} | |
}; | |
/* | |
Prompt | |
"Given an integer, take the (mean) average of each pair of consecutive digits. | |
Repeat this process until you have a single integer, then return that integer. | |
Note: if the average of two digits is not an integer, round the result up | |
(e.g. the average of 8 and 9 will be 9)." | |
*/ |
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 remove = (s, target) => { | |
if (s[s.length - 1] !== target) { | |
return s; | |
} | |
let x = 0; | |
for (let i in s) { | |
if (s[i] == target) { | |
break; | |
} | |
x += 1; | |
} | |
return s.slice(0, x) ? s.slice(0, x) : remove(s.slice(x + 1, s.length), target) | |
} | |
// removes all occurrences of target char from string if and only if the string ends in said target | |
const remove = str => { | |
let arr = str.split(""); | |
for (let i = arr.length - 1; i > 0; i--) { | |
if (arr[i] === "!") { | |
arr.pop(); | |
} | |
else { | |
break; | |
} | |
} | |
return arr.join(""); | |
} | |
/* | |
Prompt | |
"Remove all exclamation marks from the end of sentence." | |
*/ |
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
// xor using a ternary expression | |
const xor = (x, y) => x ? !y ? true : false : y ? true : false; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment