Skip to content

Commit a5bead7

Browse files
committed
Intermediate Algorithm Scripting (ex. 14 to 21)
+ Finished Intermediate Algorithm Scripting module
1 parent 7a1b86a commit a5bead7

File tree

10 files changed

+323
-10
lines changed

10 files changed

+323
-10
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Smallest Common Multiple
3+
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all
4+
sequential numbers in the range between these parameters.
5+
The range will be an array of two numbers that will not necessarily be in numerical order.
6+
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by
7+
all numbers between 1 and 3. The answer here would be 6.
8+
9+
- smallestCommons([1, 5]) should return a number.
10+
- smallestCommons([1, 5]) should return 60. 1, 2, 3, 4, 5
11+
- smallestCommons([5, 1]) should return 60.
12+
- smallestCommons([2, 10]) should return 2520.
13+
- smallestCommons([1, 13]) should return 360360.
14+
- smallestCommons([23, 18]) should return 6056820.
15+
*/
16+
function smallestCommons(arr) {
17+
const [min, max] = arr.sort((a, b) => a - b);
18+
const numberDivisors = max - min + 1;
19+
let upperBound = 1;
20+
for (let i = min; i <= max; i++) {
21+
upperBound *= i;
22+
}
23+
for (let multiple = max; multiple <= upperBound; multiple += max) {
24+
let divisorCount = 0;
25+
for (let i = min; i <= max; i++) {
26+
if (multiple % i === 0) {
27+
divisorCount += 1;
28+
}
29+
}
30+
if (divisorCount === numberDivisors) {
31+
return multiple;
32+
}
33+
}
34+
}
35+
36+
console.log(smallestCommons([1, 5]));
37+
console.log(smallestCommons([5, 1]));
38+
console.log(smallestCommons([2, 10]));
39+
console.log(smallestCommons([1, 13]));
40+
console.log(smallestCommons([23, 18]));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Drop it:
3+
Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the
4+
function func returns true when the iterated element is passed through it.
5+
Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.
6+
7+
- dropElements([1, 2, 3, 4], function(n) {return n >= 3;}) should return [3, 4].
8+
- dropElements([0, 1, 0, 1], function(n) {return n === 1;}) should return [1, 0, 1].
9+
- dropElements([1, 2, 3], function(n) {return n > 0;}) should return [1, 2, 3].
10+
- dropElements([1, 2, 3, 4], function(n) {return n > 5;}) should return [].
11+
- dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}) should return [7, 4].
12+
- dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) should return [3, 9, 2].
13+
*/
14+
function dropElements(arr, func) {
15+
let finalArr = [...arr];
16+
for (const val of arr) {
17+
if (func(val)) {
18+
return finalArr;
19+
}
20+
finalArr.splice(0, 1);
21+
}
22+
return [];
23+
}
24+
25+
console.log(dropElements([1, 2, 3], function (n) {
26+
return n < 3;
27+
}));
28+
console.log(dropElements([1, 2, 3, 4], function (n) {
29+
return n >= 3;
30+
}));
31+
console.log(dropElements([0, 1, 0, 1], function (n) {
32+
return n === 1;
33+
}));
34+
console.log(dropElements([1, 2, 3], function (n) {
35+
return n > 0;
36+
}));
37+
console.log(dropElements([1, 2, 3, 4], function (n) {
38+
return n > 5;
39+
}));
40+
console.log(dropElements([1, 2, 3, 7, 4], function (n) {
41+
return n > 3;
42+
}));
43+
console.log(dropElements([1, 2, 3, 9, 2], function (n) {
44+
return n > 2;
45+
}));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Steamroller:
3+
Flatten a nested array. You must account for varying levels of nesting.
4+
5+
- steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].
6+
- steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].
7+
- steamrollArray([1, [], [3, [[4]]]]) should return [1, 3, 4].
8+
- steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].
9+
- Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods.
10+
*/
11+
function steamrollArray(arr) {
12+
let finalArr = [];
13+
arr.forEach(item =>
14+
Array.isArray(item)
15+
? finalArr.push(...steamrollArray(item))
16+
: finalArr.push(item));
17+
return finalArr;
18+
}
19+
20+
console.log(steamrollArray([1, [2], [3, [[4]]]]));
21+
console.log(steamrollArray([[["a"]], [["b"]]]));
22+
console.log(steamrollArray([1, [], [3, [[4]]]]));
23+
console.log(steamrollArray([1, [], [3, [[4]]]]));
24+
console.log(steamrollArray([1, {}, [3, [[4]]]]));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Binary Agents:
3+
Return an English translated sentence of the passed binary string.
4+
The binary string will be space separated.
5+
6+
- binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111") should return the string Aren't bonfires fun!?
7+
- binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001") should return the string I love FreeCodeCamp!
8+
*/
9+
function binaryAgent(str) {
10+
return str.split(/\s+/)
11+
.map(binary => String.fromCharCode(parseInt(binary, 2)))
12+
.join("");
13+
}
14+
15+
console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
16+
console.log(binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001"));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Everything Be True:
3+
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
4+
In other words, you are given an array collection of objects.
5+
The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false.
6+
In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.
7+
Remember, you can access object properties through either dot notation or [] notation.
8+
9+
- truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return true.
10+
- truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return false.
11+
- truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age") should return false.
12+
- truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat") should return false.
13+
- truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastForward", "onBoat": true}], "onBoat") should return true.
14+
- truthCheck([{"single": "yes"}], "single") should return true.
15+
- truthCheck([{"single": ""}, {"single": "double"}], "single") should return false.
16+
- truthCheck([{"single": "double"}, {"single": undefined}], "single") should return false.
17+
- truthCheck([{"single": "double"}, {"single": NaN}], "single") should return false.
18+
*/
19+
function truthCheck(collection, pre) {
20+
for (let obj of collection) {
21+
if (!Boolean(obj[pre])) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
console.log(truthCheck([
29+
{"user": "Tinky-Winky", "sex": "male"},
30+
{"user": "Dipsy", "sex": "male"},
31+
{"user": "Laa-Laa", "sex": "female"},
32+
{"user": "Po", "sex": "female"}]
33+
, "sex"));
34+
console.log(truthCheck([
35+
{"user": "Tinky-Winky", "sex": "male"},
36+
{"user": "Dipsy"},
37+
{"user": "Laa-Laa", "sex": "female"},
38+
{"user": "Po", "sex": "female"}]
39+
, "sex"));
40+
console.log(truthCheck([
41+
{"user": "Tinky-Winky", "sex": "male", "age": 0},
42+
{"user": "Dipsy", "sex": "male", "age": 3},
43+
{"user": "Laa-Laa", "sex": "female", "age": 5},
44+
{"user": "Po", "sex": "female", "age": 4}]
45+
, "age"));
46+
console.log(truthCheck([
47+
{"name": "Pete", "onBoat": true},
48+
{"name": "Repeat", "onBoat": true},
49+
{"name": "FastForward", "onBoat": null}]
50+
, "onBoat"));
51+
console.log(truthCheck([
52+
{"name": "Pete", "onBoat": true},
53+
{"name": "Repeat", "onBoat": true, "alias": "Repete"},
54+
{"name": "FastForward", "onBoat": true}]
55+
, "onBoat"));
56+
console.log(truthCheck([{"single": "yes"}], "single"));
57+
console.log(truthCheck([{"single": ""}, {"single": "double"}], "single"));
58+
console.log(truthCheck([{"single": "double"}, {"single": undefined}], "single"));
59+
console.log(truthCheck([{"single": "double"}, {"single": NaN}], "single"));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Arguments Optional:
3+
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects
4+
one argument and returns the sum.
5+
For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.
6+
7+
Calling this returned function with a single argument will then return the sum:
8+
9+
var sumTwoAnd = addTogether(2);
10+
sumTwoAnd(3) returns 5.
11+
12+
If either argument isn't a valid number, return undefined.
13+
14+
- addTogether(2, 3) should return 5.
15+
- addTogether(23, 30) should return 53.
16+
- addTogether(5)(7) should return 12.
17+
- addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ") should return undefined.
18+
- addTogether(2, "3") should return undefined.
19+
- addTogether(2)([3]) should return undefined.
20+
*/
21+
function addTogether() {
22+
let [arg1, arg2] = arguments;
23+
if (!Number.isInteger(arg1) || (arg2 !== undefined && !Number.isInteger(arg2))) {
24+
return undefined;
25+
}
26+
if (!arg2) {
27+
return function (newArg) {
28+
if (Number.isInteger(newArg)) {
29+
return arg1 + newArg;
30+
}
31+
}
32+
}
33+
return arg1 + arg2;
34+
}
35+
36+
console.log(addTogether(2, 3));
37+
console.log(addTogether(23, 30));
38+
console.log(addTogether(5)(7));
39+
console.log(addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ"));
40+
console.log(addTogether(2, "3"));
41+
console.log(addTogether(2)([3]));
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Make a Person:
3+
Fill in the object constructor with the following methods below:
4+
getFirstName()
5+
getLastName()
6+
getFullName()
7+
setFirstName(first)
8+
setLastName(last)
9+
setFullName(firstAndLast)
10+
Run the tests to see the expected output for each method. The methods that take an argument must accept only one
11+
argument and it has to be a string. These methods must be the only available means of interacting with the object.
12+
13+
- No properties should be added. Object.keys(bob).length should always return 6.
14+
- bob instanceof Person should return true.
15+
- bob.firstName should return undefined.
16+
- bob.lastName should return undefined.
17+
- bob.getFirstName() should return the string Bob.
18+
- bob.getLastName() should return the string Ross.
19+
- bob.getFullName() should return the string Bob Ross.
20+
- bob.getFullName() should return the string Haskell Ross after bob.setFirstName("Haskell").
21+
- bob.getFullName() should return the string Haskell Curry after bob.setLastName("Curry").
22+
- bob.getFullName() should return the string Haskell Curry after bob.setFullName("Haskell Curry").
23+
- bob.getFirstName() should return the string Haskell after bob.setFullName("Haskell Curry").
24+
- bob.getLastName() should return the string Curry after bob.setFullName("Haskell Curry").
25+
*/
26+
const Person = function (firstAndLast) {
27+
// Only changed code below this line
28+
// Complete the method below and implement the others similarly
29+
let fullName = firstAndLast;
30+
31+
this.getFirstName = function () {
32+
return fullName.split(/\s+/)[0];
33+
};
34+
35+
this.getLastName = function () {
36+
return fullName.split(/\s+/)[1];
37+
};
38+
39+
this.getFullName = function () {
40+
return fullName;
41+
};
42+
43+
this.setFirstName = function (name) {
44+
fullName = `${name} ${fullName.split(/\s+/)[1]}`;
45+
};
46+
47+
this.setLastName = function (name) {
48+
fullName = `${fullName.split(/\s+/)[0]} ${name}`;
49+
};
50+
51+
this.setFullName = function (name) {
52+
fullName = name;
53+
};
54+
};
55+
56+
const bob = new Person('Bob Ross');
57+
console.log(bob.getFullName());
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Map the Debris
3+
Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
4+
The array will contain objects in the format {name: 'name', avgAlt: avgAlt}.
5+
You can read about orbital periods on Wikipedia (https://en.wikipedia.org/wiki/Orbital_period).
6+
The values should be rounded to the nearest whole number. The body being orbited is Earth.
7+
The radius of the earth is 6367.4447 kilometers, and the GM value of earth is 398600.4418 km3s-2.
8+
9+
- orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]) should return [{name: "sputnik", orbitalPeriod: 86400}].
10+
- orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]) should return [{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}].
11+
*/
12+
const GM = 398600.4418;
13+
const EARTH_RADIUS = 6367.4447;
14+
15+
function orbitalPeriod(arr) {
16+
return arr.map(element => ({
17+
name: element.name,
18+
orbitalPeriod: findOrbPeriod(element.avgAlt)
19+
}));
20+
}
21+
22+
function findOrbPeriod(alt) {
23+
const axis = alt + EARTH_RADIUS;
24+
return Math.round(2 * Math.PI * Math.sqrt(Math.pow(axis, 3) / GM));
25+
}
26+
27+
console.log(orbitalPeriod([{name: "sputnik", avgAlt: 35873.5553}]));
28+
console.log(orbitalPeriod([
29+
{name: "iss", avgAlt: 413.6},
30+
{name: "hubble", avgAlt: 556.7},
31+
{name: "moon", avgAlt: 378632.553}]));

09-intermediate-algorithm-scripting/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ freeCodeCamp module description:
2121
- [X] [ 10 - Sorted Union](10-sorted-union.js)
2222
- [X] [ 11 - Convert HTML Entities](11-convert-html-entities.js)
2323
- [X] [ 12 - Sum All Odd Fibonacci Numbers](12-sum-all-odd-fibonacci-numbers.js)
24-
- [ ] [ 13 - Sum All Primes](13-sum-all-primes.js)
25-
- [ ] [ 14 - Smallest Common Multiple]()
26-
- [ ] [ 15 - Drop it]()
27-
- [ ] [ 16 - Steamroller]()
28-
- [ ] [ 17 - Binary Agents]()
29-
- [ ] [ 18 - Everything Be True]()
30-
- [ ] [ 19 - Arguments Optional]()
31-
- [ ] [ 20 - Make a Person]()
32-
- [ ] [ 21 - Map the Debris]()
24+
- [X] [ 13 - Sum All Primes](13-sum-all-primes.js)
25+
- [X] [ 14 - Smallest Common Multiple](14-smallest-common-multiple.js)
26+
- [X] [ 15 - Drop it](15-drop-it.js)
27+
- [X] [ 16 - Steamroller](16-steamroller.js)
28+
- [X] [ 17 - Binary Agents](17-binary-agents.js)
29+
- [X] [ 18 - Everything Be True](18-everything-be-true.js)
30+
- [X] [ 19 - Arguments Optional](19-arguments-optional.js)
31+
- [X] [ 20 - Make a Person](20-make-a-person.js)
32+
- [X] [ 21 - Map the Debris](21-map-the-debris.js)
3333

3434
⬅️ [Back to main file](../README.md)
3535

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ freeCodeCamp course description:
1717
- [X] [ 06 - Basic Algorithm Scripting](/06-basic-algorithm-scripting/README.md)
1818
- [X] [ 07 - Object Oriented Programming](/07-object-oriented-programming/README.md)
1919
- [X] [ 08 - Functional Programming](/08-functional-programming/README.md)
20-
- [ ] [ 09 - Intermediate Algorithm Scripting](/09-intermediate-algorithm-scripting/README.md)
20+
- [X] [ 09 - Intermediate Algorithm Scripting](/09-intermediate-algorithm-scripting/README.md)
2121
- [ ] [ 10 - JavaScript Algorithms and Data Structures Projects]()
2222

2323
### Languages

0 commit comments

Comments
 (0)