-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |