From b3e31318a328ca169b21b773854398950540fdd0 Mon Sep 17 00:00:00 2001 From: Adam <24621027+WhiteDopeOnPunk@users.noreply.github.com> Date: Fri, 12 Jan 2024 07:01:06 -0500 Subject: [PATCH] add stuff --- addTogether.js | 9 +++++++++ binaryAgent.js | 16 ++++++++++++++++ dropIt.js | 12 ++++++++++++ eratosthenes.js | 15 +++++++++++++++ euclidian.js | 10 ++++++++++ makePerson.js | 28 ++++++++++++++++++++++++++++ mapDebris.js | 11 +++++++++++ palindromes.js | 8 ++++++++ romanNums.js | 30 ++++++++++++++++++++++++++++++ steamroller.js | 13 +++++++++++++ sumFibs.js | 20 ++++++++++++++++++++ truthCheck.js | 7 +++++++ 12 files changed, 179 insertions(+) create mode 100644 addTogether.js create mode 100644 binaryAgent.js create mode 100644 dropIt.js create mode 100644 eratosthenes.js create mode 100644 euclidian.js create mode 100644 makePerson.js create mode 100644 mapDebris.js create mode 100644 palindromes.js create mode 100644 romanNums.js create mode 100644 steamroller.js create mode 100644 sumFibs.js create mode 100644 truthCheck.js diff --git a/addTogether.js b/addTogether.js new file mode 100644 index 0000000..025fdf1 --- /dev/null +++ b/addTogether.js @@ -0,0 +1,9 @@ +function addTogether() { +const arr = Array.from(arguments); + return arr.some(n => typeof n !== "number") + ? undefined + : arr.length > 1 + ? arr.reduce((a, b) => a + b) + : n => (typeof n === "number" ? n + arr[0] : undefined); +} +console.log(addTogether(5)(7)); \ No newline at end of file diff --git a/binaryAgent.js b/binaryAgent.js new file mode 100644 index 0000000..a9bb162 --- /dev/null +++ b/binaryAgent.js @@ -0,0 +1,16 @@ +function binaryAgent(str) { + const binaries = str.split(' '); + const chars = [] + + for (let bin of binaries) { + chars.push(String.fromCharCode(Number.parseInt(bin, 2))) + } + const charString = chars.toString(); + + return charString.replaceAll(',',''); +} + +console.log(binaryAgent( + "01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111" +)); +// console.log(Number.parseInt('01000001')); \ No newline at end of file diff --git a/dropIt.js b/dropIt.js new file mode 100644 index 0000000..dd31259 --- /dev/null +++ b/dropIt.js @@ -0,0 +1,12 @@ +function dropElements(arr, func) { + if (arr.findIndex(func) === -1) { + return []; + } + return arr.slice(arr.findIndex(func)); +} + +console.log( + dropElements([1, 2, 3], function (n) { + return n >= 5; + }) +); diff --git a/eratosthenes.js b/eratosthenes.js new file mode 100644 index 0000000..28f9e38 --- /dev/null +++ b/eratosthenes.js @@ -0,0 +1,15 @@ +function sumPrimes(num) { + const isPrime = Array(num + 1).fill(true); + isPrime[0] = false; + isPrime[1] = false; + for (let prime = 2; prime < Math.sqrt(num); prime++) { + if (isPrime[prime]) { + for (let multiple = prime * prime; multiple <= num; multiple += prime) { + isPrime[multiple] = false; + } + } + } + return isPrime.reduce((sum, prime, index) => (prime ? sum + index : sum), 0); + // return isPrime; +} +console.log(sumPrimes(10000000)); diff --git a/euclidian.js b/euclidian.js new file mode 100644 index 0000000..0a66b2d --- /dev/null +++ b/euclidian.js @@ -0,0 +1,10 @@ +function smallestCommons(arr) { + const [min, max] = arr.sort((a, b) => a - b); + const range = Array(max - min + 1) + .fill(0) + .map((_, i) => i + min); + const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b)); + const lcm = (a, b) => (a * b) / gcd(a, b); + return range.reduce((mul, cur) => lcm(mul, cur)); +} +console.log(smallestCommons([2, 10])); diff --git a/makePerson.js b/makePerson.js new file mode 100644 index 0000000..b5db475 --- /dev/null +++ b/makePerson.js @@ -0,0 +1,28 @@ +class Person { + constructor(firstAndLast) { + let fullName = firstAndLast; + + this.getFullName = function () { + return fullName; + }; + this.getFirstName = function () { + return fullName.split(' ')[0]; + }; + this.getLastName = function () { + return fullName.split(' ')[1]; + }; + this.setFirstName = function (name) { + return fullName = name + ' ' + this.getLastName(); + }; + this.setLastName = function (name) { + return fullName = this.getFirstName() + ' ' + name; + }; + this.setFullName = function (name) { + return fullName = name; + }; + // return firstAndLast; + } +} + +const bob = new Person('Bob Ross'); +console.log(bob.getFullName()); \ No newline at end of file diff --git a/mapDebris.js b/mapDebris.js new file mode 100644 index 0000000..edf74e1 --- /dev/null +++ b/mapDebris.js @@ -0,0 +1,11 @@ +function orbitalPeriod(arr) { + var GM = 398600.4418; + var earthRadius = 6367.4447; + arr.forEach(element => { + element.orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earthRadius + element.avgAlt, 3) / GM)); + delete element.avgAlt; + }); + return arr; +} + +console.log(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])); \ No newline at end of file diff --git a/palindromes.js b/palindromes.js new file mode 100644 index 0000000..e6a6987 --- /dev/null +++ b/palindromes.js @@ -0,0 +1,8 @@ +function palindrome(str) { + const upper = str.toUpperCase(); + const letters = upper.match(/[A-Z0-9]/g); + + if (letters.toString() === letters.reverse().toString()) return true; + else return false; +} +console.log(palindrome("e_ye 3")); diff --git a/romanNums.js b/romanNums.js new file mode 100644 index 0000000..281cf72 --- /dev/null +++ b/romanNums.js @@ -0,0 +1,30 @@ +function convertToRoman(num) { + const out = []; + const roms = [ + ["M", 1000], + ["CM", 900], + ["D", 500], + ["CD", 400], + ["C", 100], + ["XC", 90], + ["L", 50], + ["XL", 40], + ["X", 10], + ["IX", 9], + ["V", 5], + ["IV", 4], + ["I", 1], + ]; + + for (let i of roms) { + if (num % i[1] < num) { + out.push(i[0]); + num -= i[1]; + } + console.log(num % i[1]); + } + + return out; +} + +console.log(convertToRoman(7)); diff --git a/steamroller.js b/steamroller.js new file mode 100644 index 0000000..51ad202 --- /dev/null +++ b/steamroller.js @@ -0,0 +1,13 @@ +const out = []; +function steamrollArray(arr) { + for (let item of arr) { + if (Array.isArray(item) === false) { + out.push(item); + } else if (Array.isArray(item)) { + steamrollArray(item); + } + } + return out; +} + +console.log(steamrollArray([1, [2], [3, [[4]]]])); diff --git a/sumFibs.js b/sumFibs.js new file mode 100644 index 0000000..5eb4e44 --- /dev/null +++ b/sumFibs.js @@ -0,0 +1,20 @@ +function sumFibs(num) { + if (num === 1) { + return 1; + } + + let fibs = [1, 1]; + + while (fibs[fibs.length - 1] <= num) { + fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]); + } + + if (fibs[fibs.length - 1] > num) { + fibs.pop(); + } + + let odds = fibs.filter((n) => n % 2); + + return odds.reduce((accumulator, currentValue) => accumulator + currentValue); +} +console.log(sumFibs(1000)); diff --git a/truthCheck.js b/truthCheck.js new file mode 100644 index 0000000..49475f3 --- /dev/null +++ b/truthCheck.js @@ -0,0 +1,7 @@ +function truthCheck(collection, pre) { + return collection.every(function (user) { + return user.hasOwnProperty(pre) && Boolean(user[pre]) + }); +} + + console.log(truthCheck([{"single": "double"}, {"single": NaN}], "single")); \ No newline at end of file