21
const largestProductOfThree = function (arr) {
let max = Math.max(...arr);
arr.splice(arr.indexOf(max), 1);
let result = arr[0] * arr[1] * max;
for(let i = 0; i < arr.length; i++){
for(let j = i+1; j < arr.length; j++){
if(arr[i] * arr[j] * max > result) {
result = arr[i] * arr[j] * max;
}
}
}
return result;
};
22
function fibonacci(n) {
let memo = [0, 1];
const recur = (n) => {
if (memo[n] !== undefined) return memo[n];
memo[n] = recur(n - 1) + recur(n - 2);
return memo[n];
}
return recur(n);
}
23
let bubbleSort = function (arr) {
for (let i = arr.length; i > 0; i--) {
noSwaps = true;
for (let j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
noSwaps = false;
}
}
if(noSwaps) break;
}
return arr;
};