JSFiddle - React, Tailwind, and code Playground

by Mingtao Sun

HTML

<button id="button">Generate Random TFN</button>
<hr/>
<div id="output"></div>

JavaScript

'use strict';

var weight = [1, 4, 3, 7, 5, 8, 6, 9];

$(function(){
	$('#button').click(function(){
  	$('#output').html(tfn());
  });
	
});

function tfn(){
	var tfn = randomTfn();
	while (tfn == null) {
		tfn = randomTfn();
	}
	return tfn;
}

function randomTfn(){	
	var tfn = '';
	var sum = 0;
	for (var i=0; i<weight.length; i++){
		var a = random();
		tfn += a;
		sum += a * weight[i];
	}
	var mod = sum % 11;
	if (mod != 10){
		tfn += mod;
		return tfn;
	}
	console.log("oops, bad luck. Regenerate");
	return null;
}; 

function random() {
    return Math.floor(Math.random() * 10);
}

console.log("tfn =" + tfn());