add stuff
This commit is contained in:
parent
adb1e760b7
commit
b3e31318a3
12 changed files with 179 additions and 0 deletions
9
addTogether.js
Normal file
9
addTogether.js
Normal file
|
@ -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));
|
16
binaryAgent.js
Normal file
16
binaryAgent.js
Normal file
|
@ -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'));
|
12
dropIt.js
Normal file
12
dropIt.js
Normal file
|
@ -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;
|
||||
})
|
||||
);
|
15
eratosthenes.js
Normal file
15
eratosthenes.js
Normal file
|
@ -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));
|
10
euclidian.js
Normal file
10
euclidian.js
Normal file
|
@ -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]));
|
28
makePerson.js
Normal file
28
makePerson.js
Normal file
|
@ -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());
|
11
mapDebris.js
Normal file
11
mapDebris.js
Normal file
|
@ -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}]));
|
8
palindromes.js
Normal file
8
palindromes.js
Normal file
|
@ -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"));
|
30
romanNums.js
Normal file
30
romanNums.js
Normal file
|
@ -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));
|
13
steamroller.js
Normal file
13
steamroller.js
Normal file
|
@ -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]]]]));
|
20
sumFibs.js
Normal file
20
sumFibs.js
Normal file
|
@ -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));
|
7
truthCheck.js
Normal file
7
truthCheck.js
Normal file
|
@ -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"));
|
Loading…
Add table
Reference in a new issue