Here we are going to explain 5 algorithms
Algorithm #1
convert from Celsius to Fahrenheit
The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
/**
* The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
*/
function convertToF(celsius) {
let fahrenheit= (celsius * 9/5)+32;
return fahrenheit;
}
convertToF(30);
Algorithm #2
Factorialize a Number
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.
/**
*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) {
let result = 1;
if (num >= 0) {
if (num === 0) result = 1;
else {
for (let i = 1; i <= num; i++) {
result=result * i;
}
}
num = result;
return num;
}
}
factorialize(5);
convertToF(30);
Algorithm #3
Find the Longest Word in a String
Find the Longest Word in a String
Return the length of the longest word in the provided sentence.
Your response should be a number.
/**
*Return the length of the longest word in the provided sentence.
*/
function findLongestWordLength(str) {
let strArray = [];
strArray = str.split(' ');
var longestWord = "";
strArray.forEach(function(word) {
if(word.length > longestWord.length) {
longestWord = word;
}
});
return longestWord.length;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Algorithm #4
Return Largest Numbers in Arrays
Return Largest Numbers in ArraysReturn an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
/**
*Return Largest Numbers in Arrays
Return an array consisting of the largest number from each provided sub-array. For simplicity,
the provided array will contain exactly 4 sub-arrays.
*/
function largestOfFour(arr) {
let largetArray = [];
arr.forEach(element => {
largetArray.push(Math.max(...element))
});
return largetArray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Algorithm 5
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.
/**
* 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");