Ad

iterative process from SICP :)
Θ(n) steps and Θ(1) memory.

Code
Diff
  • function factorial(n) {
      return factIter(1, 1, n)
    }
    
    function factIter(product, counter, max) {
      if (counter > max) {
        return product;
      } else {
        return factIter((counter * product), (counter + 1), max)
      }
    }
    • function factorial (n) {
    • if (n === 0 || n === 1) {
    • return 1;
    • function factorial(n) {
    • return factIter(1, 1, n)
    • }
    • function factIter(product, counter, max) {
    • if (counter > max) {
    • return product;
    • } else {
    • return n * factorial(n - 1);
    • return factIter((counter * product), (counter + 1), max)
    • }
    • }

factorial with stack, for big int

Code
Diff
  • function factorial (n) {
      const stack = [[n, 1]];
    
      while (stack.length > 0) {
        const [curr, result] = stack.pop();
    
        if(curr === 0 || curr === 1) {
          return result;
        }
        stack.push([curr - 1, result * curr]);
      }
    }
    • function factorial (n) {
    • if (n === 0 || n === 1) {
    • return 1;
    • } else {
    • return n * factorial(n - 1);
    • const stack = [[n, 1]];
    • while (stack.length > 0) {
    • const [curr, result] = stack.pop();
    • if(curr === 0 || curr === 1) {
    • return result;
    • }
    • stack.push([curr - 1, result * curr]);
    • }
    • }

It's binarySearch implementation on JS. This returns the index number that contains the value. If the value is not in the array, it will return -1

function binarySearch(arr, el) {
  let left = -1;
  let right = arr.length;
  
  while (right - left > 1) {
    const mid = Math.floor((left + right) / 2);  
    if (arr[mid] === el) {
      return mid;
    }
    if (arr[mid] > el) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return -1;
}

:)

Code
Diff
  • revstr = str => str.split('').reverse().join('');
    • function revstr(str) {
    • // i = str.length; newstr = "";
    • // while (i > 0) {
    • // newstr += str[i - 1]; i--;
    • // }
    • // return newstr;
    • return str.split('').reverse().join('');
    • }
    • revstr = str => str.split('').reverse().join('');

Hi! This is a recursive factorial function)

Code
Diff
  • function factorial (n) {
      if (n === 1) {
        return 1;
      } else {
        return n * factorial(n - 1);
      }
    }
    • function factorial (n) {
    • if (n <= 0) return 0
    • let i = fact = 1
    • while (i <= n) {
    • fact *= i
    • i++
    • if (n === 1) {
    • return 1;
    • } else {
    • return n * factorial(n - 1);
    • }
    • return fact
    • }
Code
Diff
  • let av = number => (number < 0) ? number * -1 : number;
    • function av(number){
    • return Math.abs(number);
    • }
    • let av = number => (number < 0) ? number * -1 : number;
Code
Diff
  • function insertionSort(arr) {
      function sortingValues(a, b) {
        if (a > b) return 1;
        if (a == b) return 0;
        if (a < b) return -1;
      }
      return arr.sort(sortingValues);
    }
    • function insertionSort(arr) {
    • // return sorted array
    • function sortingValues(a, b) {
    • if (a > b) return 1;
    • if (a == b) return 0;
    • if (a < b) return -1;
    • }
    • return arr.sort(sortingValues);
    • }