Skip to content

Commit 1ea416d

Browse files
committed
Add JavaScript files and solutions
1 parent fedffb5 commit 1ea416d

File tree

21 files changed

+363
-0
lines changed

21 files changed

+363
-0
lines changed

01-sum-all-numbers-in-a-range/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them.
2+
3+
The lowest number will not always come first. */
4+
5+
let sumAll = arr => {
6+
let sum = 0;
7+
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
8+
result += i;
9+
}
10+
return sum;
11+
}

02-diff-two-arrays/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
2+
3+
Note: You can return the array with its elements in any order. */
4+
5+
let diffArray = (arr1, arr2) => {
6+
const master = [...arr1, ...arr2];
7+
let finalArray = [];
8+
let checker = (masterArray, array) => {
9+
masterArray.forEach(elem => {
10+
let includes = arr1.includes(elem);
11+
if (!includes) {
12+
finalArray.push(elem)
13+
}
14+
})
15+
}
16+
checker(master, arr1);
17+
checker(master, arr2);
18+
return finalArray;
19+
}

03-seek-and-destroy/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
2+
3+
Note
4+
You have to use the arguments object. */
5+
6+
let destroyer = (...args) => {
7+
const initArr = args[0];
8+
const removeArr = args.slice(1,);
9+
return initArr.filter(elem => !removeArr.includes(elem))
10+
}

04-wherefore-art-thou/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
2+
3+
For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument. */
4+
5+
let whatIsInAName = (collection, source) => {
6+
const sourceKeys = Object.keys(source);
7+
const sourceKey1 = sourceKeys[0];
8+
const sourceVal1 = source[sourceKey1];
9+
let sourceKey2;
10+
let sourceVal2;
11+
if (sourceKeys.length > 1) {
12+
sourceKey2 = sourceKeys[1];
13+
sourceVal2 = source[sourceKey2];
14+
return collection.filter(obj => obj[sourceKey1] === sourceVal1 && obj[sourceKey2] === sourceVal2);
15+
} else {
16+
return collection.filter(obj => obj[sourceKey1] === sourceVal1);
17+
}
18+
}

05-spinal-tap-case/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes. */
2+
3+
let spinalCase = str => {
4+
return str.replace(/[\s_]/g, '-').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
5+
}

06-pig-latin/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Translate the provided string to pig latin.
2+
3+
Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".
4+
5+
If a word begins with a vowel you just add "way" to the end.
6+
7+
Input strings are guaranteed to be English words in all lowercase. */
8+
9+
let translatePigLatin = str => {
10+
const consonantBlock = /(^[^a, e, i, o, u]+)(\w+)/g;
11+
const vowels = /[a, e, i, o, u]/gi;
12+
if (str.search(vowels) === -1) {
13+
return str + 'ay';
14+
} else if (!(str.search(consonantBlock) === -1)) {
15+
return str.replace(consonantBlock , '$2$1ay');
16+
} else {
17+
return str + 'way';
18+
}
19+
}

07-search-and-replace/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Perform a search and replace on the sentence using the arguments provided and return the new sentence.
2+
3+
First argument is the sentence to perform the search and replace on.
4+
5+
Second argument is the word that you will be replacing (before).
6+
7+
Third argument is what you will be replacing the second argument with (after).
8+
9+
Note
10+
Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog" */
11+
12+
let myReplace = (str, before, after) => {
13+
if (before[0] === before[0].toUpperCase()) {
14+
after = after[0].toUpperCase() + after.substring(1);
15+
}
16+
return str.replace(before, after);
17+
}

08-dna-pairing/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
2+
3+
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
4+
5+
Return the provided character as the first element in each array.
6+
7+
For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]
8+
9+
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array. */
10+
11+
let pairElement = str => {
12+
const pairs = {
13+
A: ["A", "T"],
14+
T: ["T", "A"],
15+
C: ["C", "G"],
16+
G: ["G", "C"]
17+
};
18+
return [...str].map(elem => pairs[elem]);
19+
}

09-missing-letters/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Find the missing letter in the passed letter range and return it.
2+
3+
If all letters are present in the range, return undefined. */
4+
5+
let fearNotLetter = str => {
6+
const alphabet = "abcdefghiklmnopqrstuvxyz";
7+
const alphabetArray = alphabet.split('');
8+
let sliceMachine = string => {
9+
const beginning = alphabet.indexOf(string[0]);
10+
const end = alphabet.indexOf(string[string.length - 1]);
11+
return alphabet.slice(beginning, end + 1).split('');
12+
}
13+
let diffArray = (arr1, arr2) => {
14+
return arr1.filter(item => !arr1.includes(item) || !arr2.includes(item))
15+
}
16+
let result = diffArray(sliceMachine(str), str);
17+
return result[0];

10-sorted-union/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
2+
3+
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
4+
5+
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
6+
7+
Check the assertion tests for examples. */
8+
9+
let uniteUnique = (...args) => {
10+
const flatArr = args.flat(2);
11+
return [...(new Set(flatArr))];
12+
}

0 commit comments

Comments
 (0)