Skip to content

Commit 82b7380

Browse files
committed
Intermediate algo scripting
1 parent 0aadcda commit 82b7380

21 files changed

+307
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function sumAll(arr) {
2+
let sum = 0;
3+
if (arr[0]>arr[1]) {
4+
let i = arr[1];
5+
let j = arr[0];
6+
for (i; i<=j; i++){
7+
sum+=i;
8+
}
9+
}
10+
else {
11+
let i = arr[0];
12+
let j = arr[1];
13+
for (i; i<=j; i++){
14+
sum+=i;
15+
}
16+
}
17+
return sum;
18+
}
19+
20+
console.log(sumAll([1, 4]));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function diffArray(arr1, arr2) {
2+
return [...diff(arr1, arr2), ...diff(arr2, arr1)];
3+
4+
function diff(a, b) {
5+
return a.filter(item => b.indexOf(item) === -1);
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function destroyer(arr) {
2+
const valsToRemove = Array.from(arguments).slice(1);
3+
return arr.filter(function(val) {
4+
return !valsToRemove.includes(val);
5+
});
6+
}
7+
8+
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function whatIsInAName(collection, source) {
2+
const sourceKeys = Object.keys(source);
3+
4+
return collection
5+
.filter(obj => sourceKeys
6+
.every(key => obj.hasOwnProperty(key) &&
7+
obj[key] === source[key]))
8+
9+
}
10+
11+
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function spinalCase(str) {
2+
return str
3+
.split(/\s|_|(?=[A-Z])/)
4+
.join("-")
5+
.toLowerCase();
6+
}
7+
8+
console.log(spinalCase('This Is Spinal Tap'));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function translatePigLatin(str) {
2+
return str
3+
.replace(/^[aeiou]\w*/, "$&way")
4+
.replace(/(^[^aeiou]+)(\w*)/, "$2$1ay");
5+
}
6+
7+
translatePigLatin("consonant");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function myReplace(str, before, after) {
2+
const strArr = str.split(" ");
3+
const [wordToReplace] = strArr.filter(item => item === before);
4+
const replacement = wordToReplace[0] === wordToReplace[0].toUpperCase()
5+
? after[0].toUpperCase() + after.slice(1)
6+
: after[0].toLowerCase() + after.slice(1);
7+
return strArr.map(item => (item === before ? replacement : item)).join(" ");
8+
}
9+
10+
console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function pairElement(str) {
2+
const pair = {
3+
A: "T",
4+
T: "A",
5+
C: "G",
6+
G: "C"
7+
};
8+
9+
return str.split("").map(x=>[x, pair[x]]);
10+
}
11+
12+
console.log(pairElement("GCG"));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function fearNotLetter(str) {
2+
var missing;
3+
for(var i = 1; i<str.length; i++){
4+
if(str.charCodeAt(i) !== str.charCodeAt(i-1)+1){
5+
missing = String.fromCharCode(str.charCodeAt(i - 1) + 1);
6+
}
7+
}
8+
return missing;
9+
}
10+
11+
console.log(fearNotLetter("abce"));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function uniteUnique(arr) {
2+
var arr1 = [];
3+
for(var i of arguments) {
4+
for(var k of i) {
5+
if(arr1.indexOf(k) === -1) {
6+
arr1.push(k);
7+
}
8+
}
9+
}
10+
arr = arr1;
11+
return arr;
12+
//return [...new Set(Array.from(arguments).reduce((a, b) => a.concat(b)))];
13+
}
14+
15+
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

0 commit comments

Comments
 (0)