An algorithm is a series of step-by-step instructions that describe how to do something.
To write an effective algorithm, it helps to break a problem down into smaller parts and think carefully about how to solve each part with code.
This repository contains JavaScript based examples that will teach you the fundamentals of algorithmic thinking by writing functions that do everything from converting temperatures to handling complex 2D arrays.
All of these examples are from FreeCodeCamp's course.
- Basic Algorithm Scripting
- 1. Convert Celsius to Fahrenheit
- 2. Reverse a String
- 3. Factorialize a Number
- 4. Find the Longest Word in a String
- 5. Return Largest Numbers in Arrays Passed
- 6. Confirm the Ending Passed
- 7. Repeat a String Repeat a String
- 8. Truncate a String
- 9. Finders Keepers
- 10. Boo who
- 11. Title Case a Sentence
- 12. Slice and Splice
- 13. Falsy Bouncer
- 14. Where do I Belong
- 15. Mutations
Difficulty: Beginner
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
You are given a variable celsius representing a temperature in Celsius. Use the variable fahrenheit already defined and assign it the Fahrenheit temperature equivalent to the given Celsius temperature. Use the algorithm mentioned above to help convert the Celsius temperature to Fahrenheit.
function convertToF(celsius) {
let fahrenheit = (celsius * 9) / 5 + 32;
return fahrenheit;
}
convertToF(30);
Difficulty: Beginner
Reverse the provided string.
You may need to turn the string into an array before you can reverse it.
Your result must be a string.
function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("hello");
function reverseString(str) {
let reversedString = "";
for (let i = str.length - 1; i >= 0; i--) {
reversedString += str[i];
}
return reversedString;
}
reverseString("hello");
Difficulty: Beginner
Return the factorial of the provided integer.
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
Factorials are often represented with the shorthand notation n!
For example: 5! = 1 _ 2 _ 3 _ 4 _ 5 = 120
Only integers greater than or equal to zero will be supplied to the function.
function factorialize(num) {
if (num === 0) {
return 1;
} else return num * factorialize(num - 1);
}
factorialize(5);
function factorialize(num) {
let factorializedNumber = 1;
for (let i = 2; i <= num; i++) {
factorializedNumber *= i;
}
return factorializedNumber;
}
factorialize(5);
Difficulty: Beginner
Return the length of the longest word in the provided sentence.
Your response should be a number.
function findLongestWordLength(str) {
let splitWords = str.split(" ");
let longestWordLength = 0;
for (let i = 0; i < splitWords.length; i++) {
if (splitWords[i].length > longestWordLength) {
longestWordLength = splitWords[i].length;
}
}
return longestWordLength;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Difficulty: Beginner
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
function largestOfFour(arr) {
let results = [];
for (let i = 0; i < arr.length; i++) {
let largestNumber = arr[i][0];
for (let j = 1; j < arr[i].length; j++) {
if (arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
}
}
results[i] = largestNumber;
}
return results;
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1],
]);
function largestOfFour(arr) {
let results = [];
arr.forEach((elements) => {
let largestNumber = elements[0];
elements.forEach((item) => {
item > largestNumber && (largestNumber = item);
});
results.push(largestNumber);
});
return results;
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1],
]);
Difficulty: Beginner
Check if a string (first argument, str) ends with the given target string (second argument, target).
This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
function confirmEnding(str, target) {
return target === str.substr(-target.length);
}
confirmEnding("Bastian", "n");
Difficulty: Beginner
Repeat a given string str (first argument) for num times (second argument). 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.
function repeatStringNumTimes(str, num) {
if (num < 1) {
return "";
} else {
let string = str;
for (let i = 1; i < num; i++) {
string += str;
}
return string;
}
}
repeatStringNumTimes("abc", 3);
function repeatStringNumTimes(str, num) {
if (num < 1) {
return "";
} else {
return str + repeatStringNumTimes(str, num - 1);
}
}
Difficulty: Beginner
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
Difficulty: Beginner
Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'. 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.
function findElement(arr, func) {
let num = 0;
for (let i = 0; i < arr.length; i++) {
num = arr[i];
if (func(num)) {
return num;
}
}
return undefined;
}
findElement([1, 2, 3, 4], (num) => num % 2 === 0);
function findElement(arr, func) {
return arr.find(func);
}
findElement([1, 2, 3, 4], (num) => num % 2 === 0);
Difficulty: Beginner
Check if a value is classified as a boolean primitive. Return true or false.
Boolean primitives are true and false.
function booWho(bool) {
return typeof bool === "boolean";
}
booWho(null);
Difficulty: Beginner
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like the and of.
function titleCase(str) {
let convertToArray = str.toLowerCase().split(" ");
for (let i = 0; i < convertToArray.length; i++) {
convertToArray[i] =
convertToArray[i].charAt(0).toUpperCase() + convertToArray[i].slice(1);
}
return convertToArray.join(" ");
}
titleCase("I'm a little tea pot");
function titleCase(str) {
let convertToArray = str.toLowerCase().split(" ");
return convertToArray
.map((item) => {
return item.replace(item.charAt(0), item.charAt(0).toUpperCase());
})
.join(" ");
}
titleCase("I'm a little tea pot");
Difficulty: Beginner
You are given two arrays and an index.
Copy each element of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain the same after the function runs.
function frankenSplice(arr1, arr2, n) {
let localArr = [...arr2];
localArr.splice(n, 0, ...arr1);
return localArr;
}
frankenSplice([1, 2, 3], [4, 5], 1);
Difficulty: Beginner
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
Hint: Try converting each value to a Boolean.
function bouncer(arr) {
return arr.filter(Boolean);
}
bouncer([7, "ate", "", false, 9]);
function bouncer(arr) {
let newArray = [];
for (let i = 0; i < arr.length; i++) {
arr[i] && newArray.push(arr[i]);
}
return newArray;
}
bouncer([7, "ate", "", false, 9]);
Difficulty: Beginner
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
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).
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= num) return i;
}
return arr.length;
}
getIndexToIns([10, 20, 30, 40, 50], 35);
function getIndexToIns(arr, num) {
return arr.filter((val) => num > val).length;
}
getIndexToIns([10, 20, 30, 40, 50], 35);
Difficulty: Beginner
Return true
if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true
because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"]
should return false
because the string hello
does not contain a y
.
Lastly, ["Alien", "line"]
, should return true
because all of the letters in line
are present in Alien
.
function mutation(arr) {
const arr1 = arr[0].toLowerCase().split("");
const arr2 = arr[1].toLowerCase().split("");
for (let i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
return false;
}
}
return true;
}
mutation(["hello", "hey"]);