Public
Edited
Mar 10
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
divs = result
.map(({hi2lo, lo2hi, answer}, i, e) => {
const color = i === e.length - 1 && answer === 6174 ? "red" : "black";
return html`<div>${format(hi2lo)} - ${format(lo2hi)} = <span style="color: ${color}">${format(answer)}</span></div>`;
})
Insert cell
result = {
let n = number;
let attempts = 0;
let result = 0;
const rounds = [];

while (result !== 6174 && attempts < 8) {
const p = process(n);
result = p.answer;
n = result;
rounds.push(p);
attempts++;
}

return rounds;
}
Insert cell
function format(n) {
const s = n.toString();
const l = s.length;
const spaces = l === 4 ? "" : Array.from({ length: 5 - l }).map(() => "&nbsp;").join("");
return `${spaces}${commas(s)}`
}
Insert cell
function process(integer) {
const string = integer.toString();
let lo2hi = string.split("").sort();
const hi2lo = +lo2hi.slice().reverse().join("");
lo2hi = +lo2hi.join("");
return { hi2lo, lo2hi, answer: hi2lo - lo2hi};
}
Insert cell
function commas(x){
return x.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more