JSFiddle - React, Tailwind, and code Playground

by Tom M

JavaScript

for (let i = 1; i < 10; i++) {
collatz(i);
}


let counter = 0;
let tempCounter = 0;
let result = i;

function collatz(n) {
  if (n % 2 === 0) {	//if even
    let next = n/2;
    tester(next);
  } else {						//if odd
    let next = (3*n) + 1;
    tester(next);
  }
}

function tester(x, n) {
	if (x === 1) {
  counter += 1;
  console.log(counter);
  console.log(result);
  return counter;
  } else {
  counter += 1;
  collatz(x);
  }
}