Skip to content

Commit 8c5a37c

Browse files
committed
Intermediate Algorithm Scripting (ex. 1 to 8)
1 parent 7bf5968 commit 8c5a37c

9 files changed

+268
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Sum All Numbers in a Range:
3+
We'll pass you an array of two numbers.
4+
Return the sum of those two numbers plus the sum of all the numbers between them.
5+
The lowest number will not always come first.
6+
7+
For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10.
8+
9+
- sumAll([1, 4]) should return a number.
10+
- sumAll([1, 4]) should return 10.
11+
- sumAll([4, 1]) should return 10.
12+
- sumAll([5, 10]) should return 45.
13+
- sumAll([10, 5]) should return 45.
14+
*/
15+
16+
function sumAll(arr) {
17+
let sum = 0;
18+
const finalArray = [...arr].sort((a, b) => a - b);
19+
20+
for (let i = finalArray[0]; i <= finalArray[finalArray.length - 1]; i++) {
21+
sum += i;
22+
}
23+
24+
return sum;
25+
}
26+
27+
console.log(sumAll([1, 4]));
28+
console.log(sumAll([4, 1]));
29+
console.log(sumAll([5, 10]));
30+
console.log(sumAll([10, 5]));
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Diff Two Arrays:
3+
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both.
4+
In other words, return the symmetric difference of the two arrays.
5+
6+
Note: You can return the array with its elements in any order.
7+
8+
- diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) should return an array.
9+
- ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["pink wool"].
10+
- ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with one item.
11+
- ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["diorite", "pink wool"].
12+
- ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with two items.
13+
- ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return [].
14+
- ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return an empty array.
15+
- [1, 2, 3, 5], [1, 2, 3, 4, 5] should return [4].
16+
- [1, 2, 3, 5], [1, 2, 3, 4, 5] should return an array with one item.
17+
- [1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].
18+
- [1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return an array with two items.
19+
- [], ["snuffleupagus", "cookie monster", "elmo"] should return ["snuffleupagus", "cookie monster", "elmo"].
20+
- [], ["snuffleupagus", "cookie monster", "elmo"] should return an array with three items.
21+
- [1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"].
22+
- [1, "calf", 3, "piglet"], [7, "filly"] should return an array with six items.
23+
*/
24+
function diffArray(arr1, arr2) {
25+
const newArr = [];
26+
arr1
27+
.filter(value => !arr2.includes(value))
28+
.map(value => newArr.push(value));
29+
arr2
30+
.filter(value => !arr1.includes(value))
31+
.map(value => newArr.push(value));
32+
return newArr;
33+
}
34+
35+
console.log("expects: [4], returns: ", diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
36+
console.log("expects: [\"pink wool\"], returns: ", diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]));
37+
console.log("expects: [\"diorite\", \"pink wool\"], returns: ", diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]));
38+
console.log("expects: [], returns: ", diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]));
39+
console.log("expects: [\"piglet\", 4], returns: ", diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]));
40+
console.log("expects: [\"snuffleupagus\", \"cookie monster\", \"elmo\"], returns: ", diffArray([], ["snuffleupagus", "cookie monster", "elmo"]));
41+
console.log("expects: [1, \"calf\", 3, \"piglet\", 7, \"filly\"], returns: ", diffArray([1, "calf", 3, "piglet"], [7, "filly"]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Seek and Destroy:
3+
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments.
4+
Remove all elements from the initial array that are of the same value as these arguments.
5+
6+
Note: You have to use the arguments object.
7+
8+
- destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
9+
- destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
10+
- destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
11+
- destroyer([2, 3, 2, 3], 2, 3) should return [].
12+
- destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].
13+
- destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan") should return [12,92,65].
14+
*/
15+
function destroyer(arr,...args) {
16+
return arr.filter(value => !args.includes(value));
17+
}
18+
19+
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
20+
console.log(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3));
21+
console.log(destroyer([3, 5, 1, 2, 2], 2, 3, 5));
22+
console.log(destroyer([2, 3, 2, 3], 2, 3));
23+
console.log(destroyer(["tree", "hamburger", 53], "tree", 53));
24+
console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Wherefore art thou:
3+
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have
4+
matching name and value pairs (second argument).
5+
Each name and value pair of the source object has to be present in the object from the collection if it is to be
6+
included in the returned array.
7+
8+
For example, if the first argument is
9+
[{ first: "Romeo", last: "Montague" },
10+
{ first: "Mercutio", last: null },
11+
{ first: "Tybalt", last: "Capulet" }],
12+
and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument),
13+
because it contains the name and its value, that was passed on as the second argument.
14+
15+
- whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }) should return [{ first: "Tybalt", last: "Capulet" }].
16+
- whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }) should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }].
17+
- whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }].
18+
- whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) should return [{ "apple": 1, "bat": 2, "cookie": 2 }].
19+
- whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 }) should return [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }].
20+
- whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}) should return []
21+
*/
22+
function whatIsInAName(collection, source) {
23+
const arr = [];
24+
// Only changed code below this line
25+
collection
26+
.forEach(obj => {
27+
for (const key in source) {
28+
if (!(obj.hasOwnProperty(key) && obj[key] === source[key])) {
29+
return;
30+
}
31+
}
32+
arr.push(obj);
33+
})
34+
// Only changed code above this line
35+
return arr;
36+
}
37+
38+
console.log(whatIsInAName([{first: "Romeo", last: "Montague"}, {first: "Mercutio", last: null}, {
39+
first: "Tybalt",
40+
last: "Capulet"
41+
}], {last: "Capulet"}));
42+
console.log(whatIsInAName([{"apple": 1}, {"apple": 1}, {"apple": 1, "bat": 2}], {"apple": 1}));
43+
console.log(whatIsInAName([{"apple": 1, "bat": 2}, {"bat": 2}, {"apple": 1, "bat": 2, "cookie": 2}], {
44+
"apple": 1,
45+
"bat": 2
46+
}));
47+
console.log(whatIsInAName([{"apple": 1, "bat": 2}, {"apple": 1}, {"apple": 1, "bat": 2, "cookie": 2}], {
48+
"apple": 1,
49+
"cookie": 2
50+
}));
51+
console.log(whatIsInAName([{"apple": 1, "bat": 2}, {"apple": 1}, {
52+
"apple": 1,
53+
"bat": 2,
54+
"cookie": 2
55+
}, {"bat": 2}], {"apple": 1, "bat": 2}));
56+
console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}));
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Spinal Tap Case:
3+
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
4+
5+
- spinalCase("This Is Spinal Tap") should return the string this-is-spinal-tap.
6+
- spinalCase("thisIsSpinalTap") should return the string this-is-spinal-tap.
7+
- spinalCase("The_Andy_Griffith_Show") should return the string the-andy-griffith-show.
8+
- spinalCase("Teletubbies say Eh-oh") should return the string teletubbies-say-eh-oh.
9+
- spinalCase("AllThe-small Things") should return the string all-the-small-things.
10+
*/
11+
function spinalCase(str) {
12+
const finalString = str.replace(/([a-z])([A-Z])/g, "$1 $2");
13+
return finalString.replace(/\s|_/g, "-").toLowerCase();
14+
}
15+
16+
console.log(spinalCase('This Is Spinal Tap'));
17+
console.log(spinalCase('The_Andy_Griffith_Show'));
18+
console.log(spinalCase('Teletubbies say Eh-oh'));
19+
console.log(spinalCase('AllThe-small Things'));

Diff for: 09-intermediate-algorithm-scripting/06-pig-latin.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Pig Latin:
3+
Pig Latin is a way of altering English Words. The rules are as follows:
4+
If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add ay to it.
5+
If a word begins with a vowel, just add way at the end.
6+
Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.
7+
8+
- translatePigLatin("california") should return the string aliforniacay.
9+
- translatePigLatin("paragraphs") should return the string aragraphspay.
10+
- translatePigLatin("glove") should return the string oveglay.
11+
- translatePigLatin("algorithm") should return the string algorithmway.
12+
- translatePigLatin("eight") should return the string eightway.
13+
- Should handle words where the first vowel comes in the middle of the word. translatePigLatin("schwartz") should return the string artzschway.
14+
- Should handle words without vowels. translatePigLatin("rhythm") should return the string rhythmay.
15+
*/
16+
function translatePigLatin(str) {
17+
const regExp = /^[^aeiou]+/;
18+
const consonantsMatched = str.match(regExp);
19+
return consonantsMatched
20+
? str.replace(regExp, "").concat(`${consonantsMatched}ay`)
21+
: str.concat("way");
22+
}
23+
24+
console.log(translatePigLatin("consonant"));
25+
console.log(translatePigLatin("paragraphs"));
26+
console.log(translatePigLatin("glove"));
27+
console.log(translatePigLatin("algorithm"));
28+
console.log(translatePigLatin("eight"));
29+
console.log(translatePigLatin("schwartz"));
30+
console.log(translatePigLatin("rhythm"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Search and Replace:
3+
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
4+
First argument is the sentence to perform the search and replace on.
5+
Second argument is the word that you will be replacing (before).
6+
Third argument is what you will be replacing the second argument with (after).
7+
8+
Note: Preserve the case of the first character in the original word when you are replacing it.
9+
For example if you mean to replace the word Book with the word dog, it should be replaced as Dog
10+
11+
- myReplace("Let us go to the store", "store", "mall") should return the string Let us go to the mall.
12+
- myReplace("He is Sleeping on the couch", "Sleeping", "sitting") should return the string He is Sitting on the couch.
13+
- myReplace("I think we should look up there", "up", "Down") should return the string I think we should look down there.
14+
- myReplace("This has a spellngi error", "spellngi", "spelling") should return the string This has a spelling error.
15+
- myReplace("His name is Tom", "Tom", "john") should return the string His name is John.
16+
- myReplace("Let us get back to more Coding", "Coding", "algorithms") should return the string Let us get back to more Algorithms.
17+
*/
18+
function myReplace(str, before, after) {
19+
const regExp = /[A-Z]/;
20+
if (regExp.test(before.charAt(0))) {
21+
after = after.charAt(0).toUpperCase() + after.slice(1);
22+
} else {
23+
after = after.toLowerCase();
24+
}
25+
return str.replace(before, after);
26+
}
27+
28+
console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"));
29+
console.log(myReplace("Let us go to the store", "store", "mall"));
30+
console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"));
31+
console.log(myReplace("I think we should look up there", "up", "Down"));
32+
console.log(myReplace("This has a spellngi error", "spellngi", "spelling"));
33+
console.log(myReplace("His name is Tom", "Tom", "john"));
34+
console.log(myReplace("Let us get back to more Coding", "Coding", "algorithms"));
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
DNA Pairing:
3+
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
4+
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
5+
Return the provided character as the first element in each array.
6+
For example, for the input GCG, return [["G", "C"], ["C","G"], ["G", "C"]]
7+
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
8+
9+
- pairElement("ATCGA") should return [["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]].
10+
- pairElement("TTGAG") should return [["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]].
11+
- pairElement("CTCTA") should return [["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]].
12+
*/
13+
function pairElement(str) {
14+
const pairs = {
15+
A: 'T',
16+
T: 'A',
17+
C: 'G',
18+
G: 'C'
19+
}
20+
return [...str].map(char => [char, pairs[char]]);
21+
}
22+
23+
console.log(pairElement("GCG"));
24+
console.log(pairElement("ATCGA"));
25+
console.log(pairElement("TTGAG"));
26+
console.log(pairElement("CTCTA"));

Diff for: 09-intermediate-algorithm-scripting/README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ freeCodeCamp module description:
99
1010
### Exercises
1111

12-
- [ ] [ 01 - Sum All Numbers in a Range]()
13-
- [ ] [ 02 - Diff Two Arrays]()
14-
- [ ] [ 03 - Seek and Destroy]()
15-
- [ ] [ 04 - Wherefore art thou]()
16-
- [ ] [ 05 - Spinal Tap Case]()
17-
- [ ] [ 06 - Pig Latin]()
18-
- [ ] [ 07 - Search and Replace]()
19-
- [ ] [ 08 - DNA Pairing]()
12+
- [X] [ 01 - Sum All Numbers in a Range](01-sum-all-numbers-in-a-range.js)
13+
- [X] [ 02 - Diff Two Arrays](02-diff-two-arrays.js)
14+
- [X] [ 03 - Seek and Destroy](03-seek-and-destroy.js)
15+
- [X] [ 04 - Wherefore art thou](04-wherefore-art-thou.js)
16+
- [X] [ 05 - Spinal Tap Case](05-spinal-tap-case.js)
17+
- [X] [ 06 - Pig Latin](06-pig-latin.js)
18+
- [X] [ 07 - Search and Replace](07-search-and-replace.js)
19+
- [X] [ 08 - DNA Pairing](08-dna-pairing.js)
2020
- [ ] [ 09 - Missing letters]()
2121
- [ ] [ 10 - Sorted Union]()
2222
- [ ] [ 11 - Convert HTML Entities]()
@@ -31,7 +31,6 @@ freeCodeCamp module description:
3131
- [ ] [ 20 - Make a Person]()
3232
- [ ] [ 21 - Map the Debris]()
3333

34-
3534
⬅️ [Back to main file](../README.md)
3635

3736
###### Disclaimer: This repository should not be viewed as a facilitator for these courses. <br> This is intended to highlight and memorize my journey through this certification.

0 commit comments

Comments
 (0)