dollcode: decode

This commit is contained in:
41666 2024-06-18 15:22:04 -04:00
parent a03157960e
commit f341a5c3ce

View file

@ -47,12 +47,14 @@
</p> </p>
</section> </section>
<section> <section>
<p>input a number</p> <p>input a number or dollcode</p>
<input type="string" id="id" value="41666" placeholder="41666" /> <input type="string" id="id" value="41666" placeholder="41666" />
<input type="radio" id="decimal" name="radix" value="10" checked /> <input type="radio" id="decimal" name="radix" value="10" checked />
<label for="decimal">Decimal (base10)</label> <label for="decimal">Decimal (base10)</label>
<input type="radio" id="hex" name="radix" value="16" /> <input type="radio" id="hex" name="radix" value="16" />
<label for="hex">Hexadecimal (base16)</label> <label for="hex">Hexadecimal (base16)</label>
<input type="radio" id="dollcode" name="radix" value="dollcode" />
<label for="dollcode">dollcode</label>
<div> <div>
<div id="output">▖▘▌▘▘▌▌▌▖▘</div> <div id="output">▖▘▌▘▘▌▌▌▖▘</div>
</div> </div>
@ -98,6 +100,22 @@
return output.join(""); return output.join("");
}; };
const parseDollcode = (code) => {
// reverse the nibs!
const nibs = code.trim().split("").reverse();
console.log({ nibs });
return nibs
.map((nib) => (nib === "▖" ? 1 : nib === "▘" ? 2 : 3))
.reduce((acc, val, index) => {
const r = val * Math.pow(3, index);
const res = r + acc;
console.log({ acc, val, index, r, res });
return res;
}, 0);
};
const setOutput = (t) => { const setOutput = (t) => {
document.querySelector("#output").innerHTML = t; document.querySelector("#output").innerHTML = t;
}; };
@ -108,9 +126,19 @@
const updateEvent = () => { const updateEvent = () => {
const id = document.querySelector("#id").value; const id = document.querySelector("#id").value;
const number = parseInt(id, getIDRadix());
setOutput(genCode(number)); if (document.querySelector("#dollcode").checked) {
const number = parseDollcode(id);
console.log({ number });
setOutput(
`<ul><li>dec: ${number.toString(10)}</li><li>hex: ${number.toString(
16
)}</li></ul>`
);
} else {
const number = parseInt(id, getIDRadix());
setOutput(genCode(number));
}
}; };
document.querySelectorAll("input").forEach((x) => { document.querySelectorAll("input").forEach((x) => {