Skip to content

Commit 700a8af

Browse files
committed
Intermediate Algorithm Scripting (ex. 9 to 12)
1 parent 8c5a37c commit 700a8af

File tree

6 files changed

+145
-5
lines changed

6 files changed

+145
-5
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Missing letters:
3+
Find the missing letter in the passed letter range and return it.
4+
If all letters are present in the range, return undefined.
5+
6+
- fearNotLetter("abce") should return the string d.
7+
- fearNotLetter("abcdefghjklmno") should return the string i.
8+
- fearNotLetter("stvwx") should return the string u.
9+
- fearNotLetter("bcdf") should return the string e.
10+
- fearNotLetter("abcdefghijklmnopqrstuvwxyz") should return undefined.
11+
*/
12+
function fearNotLetter(str) {
13+
const strStartingCharCode = str[0].charCodeAt(0);
14+
for (let i = strStartingCharCode; i < strStartingCharCode + str.length; i++) {
15+
const alphabetCharacter = String.fromCharCode(i);
16+
if (str.charAt(i - strStartingCharCode) !== alphabetCharacter) {
17+
return alphabetCharacter;
18+
}
19+
}
20+
return undefined;
21+
}
22+
23+
console.log(fearNotLetter("abce"));
24+
console.log(fearNotLetter("abcdefghjklmno"));
25+
console.log(fearNotLetter("stvwx"));
26+
console.log(fearNotLetter("bcdf"));
27+
console.log(fearNotLetter("abcdefghijklmnopqrstuvwxyz"));
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Sorted Union:
3+
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.
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+
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
6+
Check the assertion tests for examples.
7+
8+
- uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) should return [1, 3, 2, 5, 4].
9+
- uniteUnique([1, 2, 3], [5, 2, 1]) should return [1, 2, 3, 5].
10+
- uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].
11+
*/
12+
function uniteUnique(arr) {
13+
const args = [...arguments];
14+
const output = [];
15+
16+
for (const innerArray of args) {
17+
for (const item of innerArray) {
18+
if (!output.includes(item)) {
19+
output.push(item);
20+
}
21+
}
22+
}
23+
24+
return output;
25+
}
26+
27+
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
28+
console.log(uniteUnique([1, 2, 3], [5, 2, 1]));
29+
console.log(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Convert HTML Entities
3+
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
4+
5+
- convertHTML("Dolce & Gabbana") should return the string Dolce &amp; Gabbana.
6+
- convertHTML("Hamburgers < Pizza < Tacos") should return the string Hamburgers &lt; Pizza &lt; Tacos.
7+
- convertHTML("Sixty > twelve") should return the string Sixty &gt; twelve.
8+
- convertHTML('Stuff in "quotation marks"') should return the string Stuff in &quot;quotation marks&quot;.
9+
- convertHTML("Schindler's List") should return the string Schindler&apos;s List.
10+
- convertHTML("<>") should return the string &lt;&gt;.
11+
- convertHTML("abc") should return the string abc.
12+
*/
13+
function convertHTML(str) {
14+
const htmlEntities = {
15+
"&": "&amp;",
16+
"<": "&lt;",
17+
">": "&gt;",
18+
'"': "&quot;",
19+
"'": "&apos;"
20+
};
21+
return str.replace(/[&<>"']/g, match => htmlEntities[match]);
22+
}
23+
24+
console.log(convertHTML("Dolce & Gabbana"));
25+
console.log(convertHTML("Hamburgers < Pizza < Tacos"));
26+
console.log(convertHTML("Sixty > twelve"));
27+
console.log(convertHTML('Stuff in "quotation marks"'));
28+
console.log(convertHTML("Schindler's List"));
29+
console.log(convertHTML("<>"));
30+
console.log(convertHTML("abc"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Sum All Odd Fibonacci Numbers:
3+
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
4+
The first two numbers in the Fibonacci sequence are 1 and 1.
5+
Every additional number in the sequence is the sum of the two previous numbers.
6+
The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
7+
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.
8+
9+
- sumFibs(1) should return a number.
10+
- sumFibs(1000) should return 1785.
11+
- sumFibs(4000000) should return 4613732.
12+
- sumFibs(4) should return 5.
13+
- sumFibs(75024) should return 60696.
14+
- sumFibs(75025) should return 135721.
15+
*/
16+
function sumFibs(num) {
17+
const fibonacciSequence = [1, 1];
18+
let lastFibonacciNumber = fibonacciSequence[fibonacciSequence.length - 1];
19+
20+
while (lastFibonacciNumber <= num) {
21+
lastFibonacciNumber = fibonacciSequence[fibonacciSequence.length - 1];
22+
const previousFibonacciNumber = fibonacciSequence[fibonacciSequence.length - 2];
23+
const sumLastFibonacciNumbers = previousFibonacciNumber + lastFibonacciNumber;
24+
25+
if (sumLastFibonacciNumbers > num) {
26+
break;
27+
}
28+
29+
fibonacciSequence.push(sumLastFibonacciNumbers);
30+
}
31+
32+
return fibonacciSequence
33+
.filter(number => number % 2 !== 0)
34+
.reduce((sum, b) => sum + b);
35+
}
36+
37+
console.log(sumFibs(4));
38+
console.log(sumFibs(1));
39+
console.log(sumFibs(1000));
40+
console.log(sumFibs(4000000));
41+
console.log(sumFibs(75024));
42+
console.log(sumFibs(75025));
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Sum All Primes:
3+
A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself.
4+
For example, 2 is a prime number because it is only divisible by 1 and 2.
5+
In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
6+
7+
Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.
8+
9+
- sumPrimes(10) should return a number.
10+
- sumPrimes(10) should return 17.
11+
- sumPrimes(977) should return 73156.
12+
*/

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ freeCodeCamp module description:
1717
- [X] [ 06 - Pig Latin](06-pig-latin.js)
1818
- [X] [ 07 - Search and Replace](07-search-and-replace.js)
1919
- [X] [ 08 - DNA Pairing](08-dna-pairing.js)
20-
- [ ] [ 09 - Missing letters]()
21-
- [ ] [ 10 - Sorted Union]()
22-
- [ ] [ 11 - Convert HTML Entities]()
23-
- [ ] [ 12 - Sum All Odd Fibonacci Numbers]()
24-
- [ ] [ 13 - Sum All Primes]()
20+
- [X] [ 09 - Missing letters](09-missing-letters.js)
21+
- [X] [ 10 - Sorted Union](10-sorted-union.js)
22+
- [X] [ 11 - Convert HTML Entities](11-convert-html-entities.js)
23+
- [X] [ 12 - Sum All Odd Fibonacci Numbers](12-sum-all-odd-fibonacci-numbers.js)
24+
- [ ] [ 13 - Sum All Primes](13-sum-all-primes.js)
2525
- [ ] [ 14 - Smallest Common Multiple]()
2626
- [ ] [ 15 - Drop it]()
2727
- [ ] [ 16 - Steamroller]()

0 commit comments

Comments
 (0)