dollcode: decode
This commit is contained in:
parent
a03157960e
commit
f341a5c3ce
1 changed files with 31 additions and 3 deletions
|
@ -47,12 +47,14 @@
|
|||
</p>
|
||||
</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="radio" id="decimal" name="radix" value="10" checked />
|
||||
<label for="decimal">Decimal (base10)</label>
|
||||
<input type="radio" id="hex" name="radix" value="16" />
|
||||
<label for="hex">Hexadecimal (base16)</label>
|
||||
<input type="radio" id="dollcode" name="radix" value="dollcode" />
|
||||
<label for="dollcode">dollcode</label>
|
||||
<div>
|
||||
<div id="output">▖▘▌▘▘▌▌▌▖▘</div>
|
||||
</div>
|
||||
|
@ -98,6 +100,22 @@
|
|||
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) => {
|
||||
document.querySelector("#output").innerHTML = t;
|
||||
};
|
||||
|
@ -108,9 +126,19 @@
|
|||
|
||||
const updateEvent = () => {
|
||||
const id = document.querySelector("#id").value;
|
||||
const number = parseInt(id, getIDRadix());
|
||||
|
||||
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) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue