From 0b646210c2342de565ea6f4ab642be93bd353fba Mon Sep 17 00:00:00 2001 From: dkallen78 Date: Sun, 24 Dec 2023 14:51:44 -0600 Subject: [PATCH] MoAr cLoX --- balanced-ternary-clock/README.md | 3 + balanced-ternary-clock/balanced-tern.js | 125 ++++++++++++++++++ balanced-ternary-clock/tern-time.html | 53 ++++++++ balanced-ternary-clock/tern-time.js | 119 +++++++++++++++++ .../ternary-experiment.html | 58 ++++++++ 5 files changed, 358 insertions(+) create mode 100644 balanced-ternary-clock/README.md create mode 100644 balanced-ternary-clock/balanced-tern.js create mode 100644 balanced-ternary-clock/tern-time.html create mode 100644 balanced-ternary-clock/tern-time.js create mode 100644 balanced-ternary-clock/ternary-experiment.html diff --git a/balanced-ternary-clock/README.md b/balanced-ternary-clock/README.md new file mode 100644 index 0000000..b2b24ef --- /dev/null +++ b/balanced-ternary-clock/README.md @@ -0,0 +1,3 @@ +#[Digital Ternary Clock v0.1](https://dkallen78.github.io/ternary-experiment/tern-time.html) + +It's a digital ternary clock \ No newline at end of file diff --git a/balanced-ternary-clock/balanced-tern.js b/balanced-ternary-clock/balanced-tern.js new file mode 100644 index 0000000..7a7a9b2 --- /dev/null +++ b/balanced-ternary-clock/balanced-tern.js @@ -0,0 +1,125 @@ +function rnd(floor, ceiling) { + /* + //Generates a random number within a range of numbers // + //----------------------------------------------------// + //floor(integer): lower bound of the random number // + //ceiling(integer): upper bound of the random number // + //----------------------------------------------------// + //return(integer): random number w/in the range // + */ + +let range = (ceiling - floor) + 1; +return Math.floor((Math.random() * range) + floor); +} + +function getRandom() { + let randomNumber = rnd(1, 1000); + const numberInput = document.getElementById("number-input"); + numberInput.value = randomNumber; +} + +function findHighestPower(input) { + + for (var power = 0; input >= 3 ** power; power++) { + } + + return power - 1; +} + +function convert2tern() { + + console.clear(); + let numberInput = document.getElementById("number-input").value; + let input = parseInt(numberInput, 10); + + let power = findHighestPower(input); + let output = ""; + + for (let i = power; i >= 0; i--) { + + let digit = Math.floor(input / (3 ** i)).toString(10); + output += digit; + + input = input % (3 ** i); + } + + convert2balanced(output); +} + +function convert2balanced(number) { + let unbal = [0]; + for (let i = 0; i < number.length; i++) { + unbal.push(parseInt(number[i], 10)); + } + + let start = unbal.findLastIndex((x) => x > 0); + + //adding 1s + let carry = false; + for (let i = start; i >= 0; i--) { + + if (carry) { + unbal[i]++; + carry = false; + } + + if (i > 0) { + switch(unbal[i]) { + case 0: + unbal[i] = 1; + break; + case 1: + unbal[i] = 2; + break; + case 2: + unbal[i] = 0; + carry = true; + break; + case 3: + unbal[i] = 1; + carry = true; + break; + } + } + + } + + //taking away 1s + for (let i = start; i >= 1; i--) { + + switch(unbal[i]) { + case 0: + unbal[i] = neg; + break; + case 1: + unbal[i] = zero; + break; + case 2: + unbal[i] = pos; + break; + } + } + + if (unbal[0] === 1) { + unbal[0] = pos; + } + + let output = ""; + unbal.forEach((x) => output += x); + + console.log(output); +} + +const button = document.getElementById("convert-input"); + +button.addEventListener("click", convert2tern); + +const rndButton = document.getElementById("random-number"); + +rndButton.addEventListener("click", getRandom); + +//let neg = "⥝"; +let neg = "T"; +//let pos = "⥠"; +let pos = "1"; +let zero = "0"; diff --git a/balanced-ternary-clock/tern-time.html b/balanced-ternary-clock/tern-time.html new file mode 100644 index 0000000..5a08c3e --- /dev/null +++ b/balanced-ternary-clock/tern-time.html @@ -0,0 +1,53 @@ + + + + + + + Title + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + diff --git a/balanced-ternary-clock/tern-time.js b/balanced-ternary-clock/tern-time.js new file mode 100644 index 0000000..1f4da60 --- /dev/null +++ b/balanced-ternary-clock/tern-time.js @@ -0,0 +1,119 @@ +function findHighestPower(number, base) { + + for (var power = 0; number >= base ** power; power++) { + } + + return power - 1; +} + +function toTernary(number) { + + number = parseInt(number, 10); + + let power = findHighestPower(number, 3); + let output = ""; + + for (let i = power; i >= 0; i--) { + + let digit = Math.floor(number / (3 ** i)).toString(10); + output += digit; + + number = number % (3 ** i); + } + + return output; +} + +function toBalancedTernary(number) { + let unbal = [0]; + for (let i = 0; i < number.length; i++) { + unbal.push(parseInt(number[i], 10)); + } + + let start = unbal.findLastIndex((x) => x > 0); + + //adding 1s + let carry = false; + for (let i = start; i >= 0; i--) { + + if (carry) { + unbal[i]++; + carry = false; + } + + if (i > 0) { + switch(unbal[i]) { + case 0: + unbal[i] = 1; + break; + case 1: + unbal[i] = 2; + break; + case 2: + unbal[i] = 0; + carry = true; + break; + case 3: + unbal[i] = 1; + carry = true; + break; + } + } + + } + + //taking away 1s + for (let i = start; i >= 1; i--) { + + switch(unbal[i]) { + case 0: + unbal[i] = neg; + break; + case 1: + unbal[i] = zero; + break; + case 2: + unbal[i] = pos; + break; + } + } + + if (unbal[0] === 1) { + unbal[0] = pos; + } + + let output = ""; + unbal.forEach((x) => output += x); + + return output; +} + +//let neg = "⥝"; +let neg = "T"; +//let pos = "⥠"; +let pos = "1"; +let zero = "0"; + + +let dial = document.getElementById("dial"); + +let timeInterval = setInterval(function() { + + let time = new Date(); + //console.log("running..."); + + let hours = time.getHours(); + let minutes = time.getMinutes(); + let seconds = time.getSeconds(); + + hours = toTernary(hours); + minutes = toTernary(minutes); + seconds = toTernary(seconds); + + hours = toBalancedTernary(hours).padStart(4, 0); + minutes = toBalancedTernary(minutes).padStart(5, 0); + seconds = toBalancedTernary(seconds).padStart(5, 0); + + dial.innerHTML = `${hours}:${minutes}:${seconds}`; + +}, 10); \ No newline at end of file diff --git a/balanced-ternary-clock/ternary-experiment.html b/balanced-ternary-clock/ternary-experiment.html new file mode 100644 index 0000000..25ddf5f --- /dev/null +++ b/balanced-ternary-clock/ternary-experiment.html @@ -0,0 +1,58 @@ + + + + + + + Title + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+
+ + + + +