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

+11
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

+19
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

+10
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

+18
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

+5
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

+19
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

+17
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

+19
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

+17
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

+12
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+
}

11-convert-html-entities/main.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities. */
2+
3+
let convertHTML = str => {
4+
const map = {
5+
"&": `&amp;`,
6+
"<": `&lt;`,
7+
">": `&gt;`,
8+
"\"": `&quot;`,
9+
"\'": `&apos;`
10+
}
11+
return [...str].map(letter => map[letter] || letter).join('');
12+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
2+
3+
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
4+
5+
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5. */
6+
7+
let sumFibs = num => {
8+
let a = 0;
9+
let b = 1;
10+
let result = [0, 1];
11+
while(result[result.length - 1] < num) {
12+
result.push(a+b);
13+
a = result[result.length - 2];
14+
b = result[result.length - 1];
15+
}
16+
return result
17+
.filter(el => el <= num && el % 2 === 1)
18+
.reduce((total, currentVal) => total += currentVal);
19+
}

13-sum-all-primes/main.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Sum all the prime numbers up to and including the provided number.
2+
3+
A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it's only divisible by one and two.
4+
5+
The provided number may not be a prime. */
6+
7+
let sumPrimes = num => {
8+
let isPrime = value => {
9+
for(var i = 2; i < value; i++) {
10+
if(value % i === 0) {
11+
return false;
12+
}
13+
}
14+
return value > 1;
15+
}
16+
return Array.from({length: num + 1}, (v, i) => i)
17+
.filter(isPrime)
18+
.reduce((total, currentVal) => total += currentVal);
19+
}

14-smallest-common-multiple/main.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
2+
3+
The range will be an array of two numbers that will not necessarily be in numerical order.
4+
5+
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6. */
6+
7+
let smallestCommons = arr => {
8+
let min = Math.min.apply(null, arr);
9+
let max = Math.max.apply(null, arr);
10+
let grandLCM;
11+
for (let i=min; i<max; i++) {
12+
if (i===min){
13+
grandLCM = (i * (i+1))/gcd(i, i+1);
14+
} else{
15+
grandLCM = (grandLCM * (i+1))/gcd(grandLCM, i+1);
16+
}
17+
}
18+
return grandLCM;
19+
function gcd(x, y) { // Implements The Euclidean Algorithm
20+
if (y === 0)
21+
return x;
22+
else
23+
return gcd(y, x%y);
24+
}
25+
}

15-drop-it/main.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it.
2+
3+
Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array. */
4+
5+
let dropElements = (arr, func) => {
6+
let index = arr.findIndex(func);
7+
return index === -1 ? [] : arr.slice(index);
8+
}

16-steamroller/main.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Flatten a nested array. You must account for varying levels of nesting. */
2+
3+
let deepFlatten = arr => {
4+
let flat = arr.flat();
5+
return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
6+
}

17-binary-agents/main.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Return an English translated sentence of the passed binary string.
2+
3+
The binary string will be space separated. */
4+
5+
let binaryAgent = str => {
6+
return str
7+
.split(" ")
8+
.map(elem => String.fromCharCode(parseInt(elem, 2)))
9+
.join("");
10+
}

18-everything-be-true/main.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
3+
4+
In other words, you are given an array collection of objects. The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false.
5+
6+
In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.
7+
8+
Remember, you can access object properties through either dot notation or [] notation.
9+
*/
10+
11+
let truthCheck = (collection, pre) => {
12+
return collection.every(obj => {
13+
switch (obj.hasOwnProperty(pre)) {
14+
case obj[pre] === 0:
15+
case obj[pre] === '':
16+
case obj[pre] === undefined:
17+
case obj[pre] === null:
18+
case String(obj[pre]) === "NaN":
19+
return false;
20+
default:
21+
return true;
22+
}
23+
})
24+
}
25+
let truthCheck = (collection, pre) => {
26+
return collection.every(el => el.hasOwnProperty(pre) && Boolean(el[pre]))
27+
}
28+
let truthCheck = (collection, pre) => collection.every(obj => obj[pre]);

19-arguments-optional/main.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
3+
4+
For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.
5+
6+
Calling this returned function with a single argument will then return the sum:
7+
8+
var sumTwoAnd = addTogether(2);
9+
10+
sumTwoAnd(3) returns 5.
11+
12+
If either argument isn't a valid number, return undefined.
13+
*/
14+
15+
let addTogether = (...args) => {
16+
let isNumber = elem => typeof elem === 'number' ? true : false;
17+
if (args.length > 1) {
18+
let num1 = args[0], num2 = args[1];
19+
if (isNumber(num1) && isNumber(num2)) {
20+
return num1 + num2;
21+
} else {
22+
return undefined
23+
}
24+
} else {
25+
let num3 = args[0];
26+
if (isNumber(num3)) {
27+
return function(arg2) {
28+
if (isNumber(arg2)) {
29+
return num3 + arg2;
30+
}
31+
}
32+
} else {
33+
undefined
34+
}
35+
}
36+
};

20-make-a-person/main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Fill in the object constructor with the following methods below:
3+
4+
getFirstName() getLastName() getFullName() setFirstName(first) setLastName(last) setFullName(firstAndLast)
5+
Run the tests to see the expected output for each method.
6+
7+
The methods that take an argument must accept only one argument and it has to be a string.
8+
9+
These methods must be the only available means of interacting with the object.
10+
*/
11+
12+
let Person = function(firstAndLast) {
13+
var fullName = firstAndLast;
14+
this.getFirstName = function() {
15+
return fullName.split(" ")[0];
16+
};
17+
this.getLastName = function() {
18+
return fullName.split(" ")[1];
19+
};
20+
this.getFullName = function() {
21+
return fullName;
22+
};
23+
this.setFirstName = function(name) {
24+
fullName = name + " " + fullName.split(" ")[1];
25+
};
26+
this.setLastName = function(name) {
27+
fullName = fullName.split(" ")[0] + " " + name;
28+
};
29+
this.setFullName = function(name) {
30+
fullName = name;
31+
};
32+
};
33+
let bob = new Person('Bob Ross');
34+
bob.getFullName();

0 commit comments

Comments
 (0)