JSFiddle - React, Tailwind, and code Playground

by Kristy Bond

HTML

<input type="button" value="Go" onclick="alert(doFun([1,2,3,4,5,6]));" />
<input type="button" value="Go" onclick="alert(doFun2(15))" />
<input type="button" value="Go" onclick="alert(doFun3([1,2,3,4]));" />
<input type="button" value="Go" onclick="alert(fb(4,2));" />
<input type="button" value="Go" onclick="alert(f2(17));" />

<input type="button" value="y" onclick="alert(f4(y=y));" />

<input type="button" value="17" onclick="alert(f4(y) = y));" />

JavaScript

function doFun(q) {
  // Enqueue is equivalent to push, Dequeue is equivalent to shift
  var s = [];
  while (q.length != 0) {
    s.push(q.shift());
  }
  while (s.length != 0) {
    q.push(s.pop());
  }
  return q;
}

function doFun2(n) {
  // Enqueue is equivalent to push, Dequeue is equivalent to shift
  var q = [];
  q.push(0);
  q.push(1);
  for (var i = 0; i < n; i++) {
    var a = q.shift();
    var b = q.shift();
    q.push(b);
    q.push(a + b);
  }
  return q.pop();
}

function doFun3(q) {
  // Enqueue is equivalent to push, Dequeue is equivalent to shift
  if (q.length > 0) {
    var i = q.shift();
    doFun3(q);
    q.push(i);
  }
  return q;
}

function fa(x, y) {
  if (y == 0) return 0;
  return (x + fa(x, y - 1));
}


function fb(x, y) {
  if (y == 0) return 1;
  return fa(x, fb(x, y - 1));
}

function f3(n) {
  if (n <= 1) return 1;
  if (n % 2 == 0) return f3(parseInt(n / 2));
  return f3(parseInt(n / 2)) + f3(parseInt(n / 2) + 1);
}

function f2(n) {
if (n == 0) return "";
return n % 2 + " " + f2(parseInt(n/2));
}

function f4(x){
if (Math.abs(x*x - 3) < 0.01) return x;
return f(x/2 + 1.5/x);
}