Skip to content

Commit d80728c

Browse files
committed
storing
Storing my answers from this module 😃
1 parent 6919af2 commit d80728c

21 files changed

+482
-0
lines changed

arguments-optional.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Create a function that sums two arguments together.
3+
If only one argument is provided, then return a function that expects one argument and returns the sum.
4+
5+
For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.
6+
*/
7+
8+
function addTogether() {
9+
const checkType = (number) => {
10+
if (typeof(number) === "number") {
11+
return true;
12+
} else return false;
13+
}
14+
15+
if (arguments.length > 1) {
16+
let x = arguments[0];
17+
let y = arguments[1];
18+
19+
if (checkType(x) && checkType(y)) return (x + y);
20+
} else if (arguments.length === 1) {
21+
let z = arguments[0];
22+
23+
if (checkType(arguments[0])) {
24+
return (arg2) => {
25+
if (checkType(arg2)) return (z + arg2);
26+
}
27+
}
28+
}
29+
}
30+
31+
console.log(addTogether(2)(3));

binary-agents.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Return an English translated sentence of the passed binary string.
3+
4+
The binary string will be space separated.
5+
*/
6+
7+
function binaryAgent(str) {
8+
let splitted = str.split(" ");
9+
let output = ""
10+
11+
for (let i = 0; i < splitted.length; i++) {
12+
let number = parseInt(splitted[i], 2);
13+
14+
output += String.fromCharCode(number);
15+
}
16+
17+
return output;
18+
}
19+
20+
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

convert-html-entities.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
3+
*/
4+
5+
function convertHTML(str) {
6+
str = str.replace(/&/g, "&amp;")
7+
str = str.replace(/</g, "&lt;")
8+
str = str.replace(/>/g, "&gt;")
9+
str = str.replace(/"/g, "&quot;")
10+
str = str.replace(/'/g, "&apos;")
11+
12+
return str;
13+
}
14+
15+
console.log(convertHTML("Hamburgers < Pizza < Tacos"));

diff-two-arrays.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
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.
3+
*/
4+
5+
const filterArray = (arr, arr2) => {
6+
let newArray = arr.filter((value) => {
7+
if (arr2.indexOf(value) < 0) return value;
8+
});
9+
10+
return newArray;
11+
}
12+
13+
function diffArray(arr1, arr2) {
14+
var newArr = [...filterArray(arr2, arr1), ...filterArray(arr1, arr2)];
15+
16+
return newArr;
17+
}
18+
19+
diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);

dna-pairing.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
3+
4+
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
5+
6+
Return the provided character as the first element in each array.
7+
*/
8+
9+
const search = (char, pairs) => {
10+
switch (char) {
11+
case "T":
12+
pairs.push(["T", "A"]);
13+
break;
14+
case "A":
15+
pairs.push(["A", "T"]);
16+
break;
17+
case "C":
18+
pairs.push(["C", "G"]);
19+
break;
20+
case "G":
21+
pairs.push(["G", "C"]);
22+
break;
23+
}
24+
}
25+
26+
function pairElement(str) {
27+
var pairs = [];
28+
29+
for (let i = 0; i < str.length; i++) {
30+
search(str[i], pairs);
31+
}
32+
33+
return pairs;
34+
}
35+
36+
pairElement("GCG");

drop-it.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
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.
3+
*/
4+
5+
function dropElements(arr, func) {
6+
let updatedArray = [];
7+
8+
for (let i = 0; i < arr.length; i++) {
9+
if (func(arr[i])) {
10+
updatedArray = arr.slice(i, arr.length);
11+
break;
12+
}
13+
}
14+
15+
return updatedArray;
16+
}
17+
18+
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3; }));

everything-be-true.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
3+
*/
4+
5+
function truthCheck(collection, pre) {
6+
for (let i = 0; i < collection.length; i++) {
7+
if (!(collection[i][pre])) return false;
8+
}
9+
10+
return true;
11+
}
12+
13+
console.log(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));

make-a-person.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Fill in the object constructor with the following methods below:
3+
4+
getFirstName()
5+
getLastName()
6+
getFullName()
7+
setFirstName(first)
8+
setLastName(last)
9+
setFullName(firstAndLast)
10+
*/
11+
12+
var Person = function(firstAndLast) {
13+
this.getFirstName = () => {
14+
let first = firstAndLast.split(" ")[0];
15+
16+
return first;
17+
}
18+
this.getLastName = () => {
19+
let last = firstAndLast.split(" ")[1];
20+
21+
return last;
22+
}
23+
this.getFullName = () => {
24+
return firstAndLast;
25+
};
26+
this.setFirstName = (name) => {
27+
firstAndLast = firstAndLast.replace(this.getFirstName(), name);
28+
};
29+
this.setLastName = (name) => {
30+
firstAndLast = firstAndLast.replace(this.getLastName(), name);
31+
};
32+
this.setFullName = (name) => {
33+
firstAndLast = name;
34+
};
35+
};
36+
37+
var bob = new Person('Bob Ross');
38+
bob.getFullName();

map-the-debris.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
3+
4+
The array will contain objects in the format {name: 'name', avgAlt: avgAlt}.
5+
*/
6+
7+
function orbitalPeriod(arr) {
8+
var GM = 398600.4418;
9+
var earthRadius = 6367.4447;
10+
11+
for (let i = 0; i < arr.length; i++) {
12+
let orbitalPeriodCalc = (2 * Math.PI) * Math.sqrt(
13+
Math.pow(earthRadius + arr[i]['avgAlt'], 3) / GM
14+
)
15+
16+
delete arr[i]['avgAlt'];
17+
18+
arr[i]['orbitalPeriod'] = Math.round(orbitalPeriodCalc);
19+
}
20+
21+
return arr;
22+
}
23+
24+
console.log(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]));

missing-letters.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Find the missing letter in the passed letter range and return it.
3+
4+
If all letters are present in the range, return undefined.
5+
*/
6+
7+
function fearNotLetter(str) {
8+
for (let i = 0; i < str.length - 1; i++) {
9+
let actualLetterCode = str.charCodeAt(i);
10+
let nextLetterCode = str.charCodeAt(i + 1);
11+
12+
if ((actualLetterCode + 1) != nextLetterCode) {
13+
return String.fromCharCode(actualLetterCode + 1);
14+
}
15+
}
16+
}
17+
18+
console.log(fearNotLetter("abce"));

pig-latin.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Translate the provided string to pig latin.
3+
4+
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".
5+
6+
If a word begins with a vowel you just add "way" to the end.
7+
8+
If a word does not contain a vowel, just add "ay" to the end.
9+
10+
Input strings are guaranteed to be English words in all lowercase.
11+
*/
12+
13+
function translatePigLatin(str) {
14+
let startVowel = /^[aeiou]/i;
15+
let globalVowels = /[aeiou]/gi;
16+
17+
if (startVowel.test(str)) {
18+
str = str.concat("way");
19+
} else if (globalVowels.test(str)) {
20+
for (let i = 0; i < str.length - 1; i++) {
21+
if (!(startVowel.test(str))) {
22+
str = str.slice(1, str.length).concat(str.charAt(0));
23+
} else {
24+
break;
25+
}
26+
}
27+
28+
str = str.concat("ay");
29+
} else {
30+
str = str.concat("ay");
31+
}
32+
33+
return str;
34+
}
35+
36+
console.log(translatePigLatin("glove"));

search-and-replace.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
3+
4+
First argument is the sentence to perform the search and replace on.
5+
6+
Second argument is the word that you will be replacing (before).
7+
8+
Third argument is what you will be replacing the second argument with (after).
9+
10+
Note
11+
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"
12+
*/
13+
14+
15+
function myReplace(str, before, after) {
16+
let regex = /^[A-Z]/
17+
18+
if (regex.test(before)) {
19+
after = after.charAt(0).toUpperCase() + after.slice(1);
20+
}
21+
22+
str = str.replace(before, after);
23+
24+
return str;
25+
}
26+
27+
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

seek-and-destroy.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
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.
3+
4+
Note
5+
You have to use the arguments object.
6+
*/
7+
8+
const destroy = (arr, number) => {
9+
arr = arr.filter(value => {
10+
if (number !== value) return value;
11+
});
12+
13+
return arr;
14+
}
15+
16+
function destroyer(arr) {
17+
let keys = Object.keys(arguments);
18+
19+
for (let i = 1; i < keys.length; i++) {
20+
arr = destroy(arr, arguments[i]);
21+
}
22+
23+
return arr;
24+
}
25+
26+
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

smallest-common-multiple.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
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.
3+
*/
4+
5+
function smallestCommons(arr) {
6+
var range = [];
7+
for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) {
8+
range.push(i);
9+
}
10+
11+
var lcm = range[0];
12+
for (i = 1; i < range.length; i++) {
13+
var GCD = gcd(lcm, range[i]);
14+
lcm = (lcm * range[i]) / GCD;
15+
}
16+
return lcm;
17+
18+
function gcd(x, y) {
19+
if (y === 0) return x;
20+
else return gcd(y, x % y);
21+
}
22+
}
23+
24+
smallestCommons([1,5]);

sorted-union.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
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.
3+
4+
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
5+
6+
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
7+
*/
8+
9+
function onlyUnique(value, index, self) {
10+
return self.indexOf(value) === index;
11+
}
12+
13+
function uniteUnique(arr) {
14+
let unique = []
15+
16+
for (let key in arguments) {
17+
unique.push(...arguments[key]);
18+
}
19+
20+
unique = unique.filter(onlyUnique);
21+
22+
return unique;
23+
}
24+
25+
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

spinal-tap-case.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
3+
*/
4+
5+
function spinalCase(str) {
6+
var regex = /\s+|_+/g;
7+
8+
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
9+
str = str.replace(regex, "-").toLowerCase();
10+
11+
return str;
12+
}
13+
14+
console.log(spinalCase('This Is Spinal Tap'));

0 commit comments

Comments
 (0)