Skip to content

Commit 5113d4e

Browse files
committed
Basic Algorithm Scripting (ex. 1 to 16)
+ Finished Basic Algorithm Scripting module
1 parent 2c53a4b commit 5113d4e

18 files changed

+448
-17
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Convert Celsius to Fahrenheit:
3+
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
4+
You are given a variable celsius representing a temperature in Celsius.
5+
Use the variable fahrenheit already defined and assign it the Fahrenheit temperature equivalent to the given Celsius temperature.
6+
Use the algorithm mentioned above to help convert the Celsius temperature to Fahrenheit.
7+
8+
- convertToF(0) should return a number
9+
- convertToF(-30) should return a value of -22
10+
- convertToF(-10) should return a value of 14
11+
- convertToF(0) should return a value of 32
12+
- convertToF(20) should return a value of 68
13+
- convertToF(30) should return a value of 86
14+
*/
15+
function convertToF(celsius) {
16+
return celsius * (9/5) + 32;
17+
}
18+
19+
console.log(convertToF(30));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Reverse a String:
3+
Reverse the provided string.
4+
You may need to turn the string into an array before you can reverse it.
5+
Your result must be a string.
6+
7+
- reverseString("hello") should return a string.
8+
- reverseString("hello") should return the string olleh.
9+
- reverseString("Howdy") should return the string ydwoH.
10+
- reverseString("Greetings from Earth") should return the string htraE morf sgniteerG.
11+
*/
12+
function reverseString(str) {
13+
let finalStr = "";
14+
for (let i = str.length - 1; i >= 0; i--) {
15+
finalStr += str[i];
16+
}
17+
return finalStr;
18+
}
19+
20+
console.log(reverseString("hello"));
21+
console.log(reverseString("Greetings from Earth"));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Factorialize a Number:
3+
Return the factorial of the provided integer.
4+
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
5+
Factorials are often represented with the shorthand notation n!
6+
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
7+
Only integers greater than or equal to zero will be supplied to the function.
8+
9+
- factorialize(5) should return a number.
10+
- factorialize(5) should return 120.
11+
- factorialize(10) should return 3628800.
12+
- factorialize(20) should return 2432902008176640000.
13+
- factorialize(0) should return 1.
14+
*/
15+
function factorialize(num) {
16+
let total = 1;
17+
while (num >= 1) {
18+
total *= num;
19+
num--;
20+
}
21+
return total;
22+
}
23+
24+
console.log(factorialize(5));
25+
console.log(factorialize(10));
26+
console.log(factorialize(20));
27+
console.log(factorialize(0));
28+
29+
30+
function factorializeRecursive(num) {
31+
if (num === 0) {
32+
return 1;
33+
}
34+
return num * factorializeRecursive(num - 1);
35+
}
36+
console.log(factorializeRecursive(5));
37+
console.log(factorializeRecursive(10));
38+
console.log(factorializeRecursive(20));
39+
console.log(factorializeRecursive(0));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Find the Longest Word in a String
3+
Return the length of the longest word in the provided sentence.
4+
Your response should be a number.
5+
6+
- findLongestWordLength("The quick brown fox jumped over the lazy dog") should return a number.
7+
- findLongestWordLength("The quick brown fox jumped over the lazy dog") should return 6.
8+
- findLongestWordLength("May the force be with you") should return 5.
9+
- findLongestWordLength("Google do a barrel roll") should return 6.
10+
- findLongestWordLength("What is the average airspeed velocity of an unladen swallow") should return 8.
11+
- findLongestWordLength("What if we try a super-long word such as otorhinolaryngology") should return 19.
12+
*/
13+
function findLongestWordLength(str) {
14+
let strToArr = str.split(" ");
15+
let longestLength = 0;
16+
for (let str of strToArr) {
17+
if (longestLength < str.length) {
18+
longestLength = str.length;
19+
}
20+
}
21+
return longestLength;
22+
}
23+
24+
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));
25+
console.log(findLongestWordLength("What is the average airspeed velocity of an unladen swallow"));
26+
console.log(findLongestWordLength("What if we try a super-long word such as otorhinolaryngology"));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Return Largest Numbers in Arrays:
3+
Return an array consisting of the largest number from each provided sub-array.
4+
For simplicity, the provided array will contain exactly 4 sub-arrays.
5+
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
6+
7+
- largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array.
8+
- largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001].
9+
- largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
10+
- largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
11+
*/
12+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Confirm the Ending:
3+
Check if a string (first argument, str) ends with the given target string (second argument, target).
4+
This challenge can be solved with the .endsWith() method, which was introduced in ES2015.
5+
But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
6+
7+
- confirmEnding("Bastian", "n") should return true.
8+
- confirmEnding("Congratulation", "on") should return true.
9+
- confirmEnding("Connor", "n") should return false.
10+
- confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
11+
- confirmEnding("He has to give me a new name", "name") should return true.
12+
- confirmEnding("Open sesame", "same") should return true.
13+
- confirmEnding("Open sesame", "sage") should return false.
14+
- confirmEnding("Open sesame", "game") should return false.
15+
- confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
16+
- confirmEnding("Abstraction", "action") should return true.
17+
- Your code should not use the built-in method .endsWith() to solve the challenge.
18+
*/
19+
function confirmEnding(str, target) {
20+
return str.substr(str.length - target.length) === target;
21+
}
22+
23+
console.log(confirmEnding("Bastian", "n"));
24+
console.log(confirmEnding("Congratulation", "on"));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Repeat a String Repeat a String:
3+
Repeat a given string str (first argument) for num times (second argument).
4+
Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.
5+
6+
- repeatStringNumTimes("*", 3) should return the string ***.
7+
- repeatStringNumTimes("abc", 3) should return the string abcabcabc.
8+
- repeatStringNumTimes("abc", 4) should return the string abcabcabcabc.
9+
- repeatStringNumTimes("abc", 1) should return the string abc.
10+
- repeatStringNumTimes("*", 8) should return the string ********.
11+
- repeatStringNumTimes("abc", -2) should return an empty string ("").
12+
- The built-in repeat() method should not be used.
13+
- repeatStringNumTimes("abc", 0) should return "".
14+
*/
15+
function repeatStringNumTimes(str, num) {
16+
let finalStr = "";
17+
while (num > 0 ) {
18+
finalStr += str;
19+
num--;
20+
}
21+
return finalStr;
22+
}
23+
24+
console.log(repeatStringNumTimes("abc", 3));
25+
console.log(repeatStringNumTimes("*", 3));
26+
console.log(repeatStringNumTimes("abc", -2));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Truncate a String:
3+
Truncate a string (first argument) if it is longer than the given maximum string length (second argument).
4+
Return the truncated string with a ... ending.
5+
6+
- truncateString("A-tisket a-tasket A green and yellow basket", 8) should return the string A-tisket....
7+
- truncateString("Peter Piper picked a peck of pickled peppers", 11) should return the string Peter Piper....
8+
- truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return the string A-tisket a-tasket A green and yellow basket.
9+
- truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return the string A-tisket a-tasket A green and yellow basket.
10+
- truncateString("A-", 1) should return the string A....
11+
- truncateString("Absolutely Longer", 2) should return the string Ab....
12+
*/
13+
function truncateString(str, num) {
14+
return num < str.length ? `${str.substring(0, num)}...` : str;
15+
}
16+
17+
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
18+
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));
19+
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length));
20+
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2));
21+
console.log(truncateString("A-", 1));
22+
console.log(truncateString("Absolutely Longer", 2));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Finders Keepers:
3+
Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'.
4+
This means that given an element x, the 'truth test' is passed if func(x) is true. If no element passes the test, return undefined.
5+
6+
- findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }) should return 8.
7+
- findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }) should return undefined.
8+
*/
9+
function findElement(arr, func) {
10+
for (let item of arr) {
11+
if (func(item)) {
12+
return item;
13+
}
14+
}
15+
return undefined;
16+
}
17+
18+
console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));
19+
console.log(findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Boo who:
3+
Check if a value is classified as a boolean primitive. Return true or false.
4+
Boolean primitives are true and false.
5+
6+
- booWho(true) should return true.
7+
- booWho(false) should return true.
8+
- booWho([1, 2, 3]) should return false.
9+
- booWho([].slice) should return false.
10+
- booWho({ "a": 1 }) should return false.
11+
- booWho(1) should return false.
12+
- booWho(NaN) should return false.
13+
- booWho("a") should return false.
14+
- booWho("true") should return false.
15+
- booWho("false") should return false.
16+
*/
17+
function booWho(bool) {
18+
return bool === true || bool === false;
19+
}
20+
21+
console.log(booWho(null));
22+
console.log(booWho(true));
23+
console.log(booWho(false));
24+
console.log(booWho([1, 2, 3]));
25+
console.log(booWho([].slice));
26+
console.log(booWho({ "a": 1 }));
27+
console.log(booWho(1 ));
28+
console.log(booWho(NaN));
29+
console.log(booWho("a"));
30+
console.log(booWho("true"));
31+
console.log(booWho("false"));
32+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Title Case a Sentence:
3+
Return the provided string with the first letter of each word capitalized.
4+
Make sure the rest of the word is in lower case.
5+
For the purpose of this exercise, you should also capitalize connecting words like the and of.
6+
7+
- titleCase("I'm a little tea pot") should return a string.
8+
- titleCase("I'm a little tea pot") should return the string I'm A Little Tea Pot.
9+
- titleCase("sHoRt AnD sToUt") should return the string Short And Stout.
10+
- titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return the string Here Is My Handle Here Is My Spout.
11+
*/
12+
function titleCase(str) {
13+
let finalStr = "";
14+
const space_separator = " ";
15+
let arr = str.split(space_separator);
16+
for (let i = 0; i < arr.length; i++) {
17+
const firstLetter = arr[i][0].toUpperCase();
18+
const restOfWord = arr[i].substring(1, arr[i].length).toLowerCase();
19+
finalStr += firstLetter + restOfWord;
20+
if (i < arr.length - 1) {
21+
finalStr += space_separator;
22+
}
23+
}
24+
return finalStr;
25+
}
26+
27+
console.log(titleCase("I'm a little tea pot"));
28+
console.log(titleCase("sHoRt AnD sToUt"));
29+
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Slice and Splice:
3+
You are given two arrays and an index.
4+
Copy each element of the first array into the second array, in order.
5+
Begin inserting elements at index n of the second array.
6+
Return the resulting array. The input arrays should remain the same after the function runs.
7+
8+
- frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5].
9+
- frankenSplice([1, 2], ["a", "b"], 1) should return ["a", 1, 2, "b"].
10+
- frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2) should return ["head", "shoulders", "claw", "tentacle", "knees", "toes"].
11+
- All elements from the first array should be added to the second array in their original order. frankenSplice([1, 2, 3, 4], [], 0) should return [1, 2, 3, 4].
12+
- The first array should remain the same after the function runs.
13+
- The second array should remain the same after the function runs.
14+
*/
15+
function frankenSplice(arr1, arr2, n) {
16+
let arr = [...arr2];
17+
arr.splice(n, 0, ...arr1)
18+
return arr;
19+
}
20+
21+
console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
22+
console.log(frankenSplice([1, 2], ["a", "b"], 1));
23+
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));
24+
console.log(frankenSplice([1, 2, 3, 4], [], 0));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Falsy Bouncer:
3+
Remove all falsy values from an array.
4+
Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
5+
Hint: Try converting each value to a Boolean.
6+
7+
- bouncer([7, "ate", "", false, 9]) should return [7, "ate", 9].
8+
- bouncer(["a", "b", "c"]) should return ["a", "b", "c"].
9+
- bouncer([false, null, 0, NaN, undefined, ""]) should return [].
10+
- bouncer([null, NaN, 1, 2, undefined]) should return [1, 2].
11+
*/
12+
function bouncer(arr) {
13+
const finalArray = [];
14+
for (const item of arr) {
15+
if (Boolean(item)) {
16+
finalArray.push(item);
17+
}
18+
}
19+
return finalArray;
20+
}
21+
22+
console.log(bouncer([7, "ate", "", false, 9]));
23+
console.log(bouncer(["a", "b", "c"]));
24+
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
25+
console.log(bouncer([null, NaN, 1, 2, undefined]));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Where do I Belong:
3+
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has
4+
been sorted. The returned value should be a number.
5+
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
6+
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20]
7+
and 19 is less than 20 (index 2) and greater than 5 (index 1).
8+
9+
- getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.
10+
- getIndexToIns([10, 20, 30, 40, 50], 35) should return a number.
11+
- getIndexToIns([10, 20, 30, 40, 50], 30) should return 2.
12+
- getIndexToIns([10, 20, 30, 40, 50], 30) should return a number.
13+
- getIndexToIns([40, 60], 50) should return 1.
14+
- getIndexToIns([40, 60], 50) should return a number.
15+
- getIndexToIns([3, 10, 5], 3) should return 0.
16+
- getIndexToIns([3, 10, 5], 3) should return a number.
17+
- getIndexToIns([5, 3, 20, 3], 5) should return 2.
18+
- getIndexToIns([5, 3, 20, 3], 5) should return a number.
19+
- getIndexToIns([2, 20, 10], 19) should return 2.
20+
- getIndexToIns([2, 20, 10], 19) should return a number.
21+
- getIndexToIns([2, 5, 10], 15) should return 3.
22+
- getIndexToIns([2, 5, 10], 15) should return a number.
23+
- getIndexToIns([], 1) should return 0.
24+
- getIndexToIns([], 1) should return a number.
25+
*/
26+
function getIndexToIns(arr, num) {
27+
arr.sort((a, b) => a - b);
28+
for (let i = 0; i < arr.length; i++) {
29+
if (arr[i] >= num)
30+
return i;
31+
}
32+
return arr.length;
33+
}
34+
35+
console.log(getIndexToIns([40, 60], 50));
36+
console.log(getIndexToIns([10, 20, 30, 40, 50], 35));
37+
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
38+
console.log(getIndexToIns([40, 60], 50));
39+
console.log(getIndexToIns([3, 10, 5], 3));
40+
console.log(getIndexToIns([5, 3, 20, 3], 5));
41+
console.log(getIndexToIns([2, 20, 10], 19));
42+
console.log(getIndexToIns([2, 5, 10], 15));
43+
console.log(getIndexToIns([], 1));

0 commit comments

Comments
 (0)