Skip to content

Commit

Permalink
MoAr cLoX
Browse files Browse the repository at this point in the history
  • Loading branch information
dkallen78 committed Dec 24, 2023
1 parent e838bae commit 0b64621
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 0 deletions.
3 changes: 3 additions & 0 deletions balanced-ternary-clock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[Digital Ternary Clock v0.1](https://dkallen78.github.io/ternary-experiment/tern-time.html)

It's a digital ternary clock
125 changes: 125 additions & 0 deletions balanced-ternary-clock/balanced-tern.js
Original file line number Diff line number Diff line change
@@ -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";
53 changes: 53 additions & 0 deletions balanced-ternary-clock/tern-time.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Title
</title>
<!--
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="stylesheet.css" media="print">
-->
<!--
web apps only
<meta name="application-name" content="Application Name">
-->
<meta name="description" content="description">

<meta property="og:title" content="Title">
<meta property="og:description" content="description">
<meta property="og:image" content="xxx.jpg">
<meta property="og:locale" content="en_US">
<!--es_MX-->
<meta property="og:type" content="website">
<!--
music.song/album/playlist/radio_station
video.movie/episode/tv_show/other
article
book
profile
website
-->
<meta property="og:url" content="https://www.site.com/page">

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="">
<meta name="twitter:creator" content="">


<style>
#dial {
font-family: monospace;
}
</style>
</head>
<body>
<div id="dial"></div>
</body>

<script src="tern-time.js">
</script>

</html>
119 changes: 119 additions & 0 deletions balanced-ternary-clock/tern-time.js
Original file line number Diff line number Diff line change
@@ -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);
58 changes: 58 additions & 0 deletions balanced-ternary-clock/ternary-experiment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Title
</title>
<!--
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="stylesheet.css" media="print">
-->
<!--
web apps only
<meta name="application-name" content="Application Name">
-->
<meta name="description" content="description">

<meta property="og:title" content="Title">
<meta property="og:description" content="description">
<meta property="og:image" content="xxx.jpg">
<meta property="og:locale" content="en_US">
<!--es_MX-->
<meta property="og:type" content="website">
<!--
music.song/album/playlist/radio_station
video.movie/episode/tv_show/other
article
book
profile
website
-->
<meta property="og:url" content="https://www.site.com/page">

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="">
<meta name="twitter:creator" content="">


<style>
</style>
</head>
<body>
<label for="number-input">Number:</label>
<br>
<input type="number" id="number-input" name="number-input" />
<br>
<input type="button" id="convert-input" value="Convert">
<br>
<input type="button" id="random-number" value="Random">
<br>
<div id="number-output"></div>
</body>

<script src="balanced-tern.js">
</script>

</html>

0 comments on commit 0b64621

Please sign in to comment.