-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.js
61 lines (55 loc) · 1.79 KB
/
algorithms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// algorithm solutions for basic algorithms scripting challenges in FCC
// Convert Celsius to Fahrenheit
function convertToF(celsius) {
let fahrenheit = celsius * (9 / 5) + 32;
return fahrenheit;
}
// Reverse a String
function reverseString(str) {
let newStr = str.split('').reverse().join('');
return newStr;
}
// Factorialize a Number. Only run positive numbers and if 0, return 1.
function factorialize(num) {
let Solution = 1;
// if number is less than 0
if(num < 0){
return "Error";
// if number is greater than 0
} else if(num > 0) {
for(let i = 1; i <= num; i++) {
Solution *= i;
}
return Solution;
// if number is 0
} else {
return Solution;
}
}
// Find the longest word in a string
function findLongestWordLength(str) {
// Split the string into words and save to an array
let longestWord = str.split(" ");
// Declare a variable to store the longest word length
let longestWordLength = 0;
// Loop through array of words
for (let word of longestWord) {
// Ternary statement that checks the word length against the current longest word length and replace if current word length is bigger
(word.length > longestWordLength) ? longestWordLength = word.length
// Keep current longest word length if the word being checked is smaller
: longestWordLength;
}
// Return the number of letters in the longest word in a string
return longestWordLength;
}
// loop through an array of subarrays and return largest number of each subarray in new array
function largestOfFour(arr) {
// new array for largest numbers
const newArr = [];
// loop through arrays and sort highest to lowest
arr.forEach(subarr => {subarr.sort( (a, b) => b - a )
// push highest to new array
newArr.push(subarr[0]);
});
return newArr;
}