uniquexiaobai
12/10/2018 - 2:23 PM

digital-root.js

function digitalRoot(num) {
	while (num > 9) {
		num = ~~(num / 10) + num % 10;
	}

	return num;
}

function digitalRoot2(num) {
	return (num - 1) % 9 + 1;
}

const numList = Array.from({length: 10}).map(x => ~~(Math.random() * 100));

numList.forEach(num => console.log(num, digitalRoot(num), digitalRoot(num) === digitalRoot2(num)))