Context
I have a snippet of code which allows a user to click on a 'variable (arbitrary) element', be prompted for a number, and see a processed version of that number produced in/on the element they clicked, based on a 'variable function'. I'll provide a more thorough description and the original code below. Skip to the bottom for the questions.
Thorough description
I have a function that looks like square(root) { return root * root; }
. Obviously it takes a number and returns that number times itself. I've got another one like this printNum(input) { return input; }
-Hopefully no explanation is needed.
The browser calls them with an event listener aimed at var element = document.getElementById("unique-key")
which looks like element.addEventListener('click', promptFunction);
.
And promptFunction uses var functionPlug =
[functionsListedAbove];
to point input from window.prompt('inputvar')
to element.innerHTML
Original Code
//where to write
var element = document.getElementById("module");
//what to write
var functionPlug = printNum;
//how to write
function square(root) { return root * root; }
function printNum(input) { return input * input / input; }
//write
function promptFunction() { element.innerHTML = functionPlug(window.prompt('inputvar')); }
//When to write
element.addEventListener('click', promptFunction);
//promptFunction(); --immediately
body {
background-color: #3A3C3D; /*alt color #CCA #3A3C3D*/
margin: 0;
overflow: hidden; /*top stop the extended shadow element height from causing the page to scroll*/
}
.backDrop {
background-color: #FFF; /*alt colors #ACA null #CCA*/
margin: auto;
height: 100vh; width: 720px;
}
.backDrop:before {
box-shadow: 0 0 20px black;
content:'';
position: absolute;
height: 200vh; width: 720px;
margin: auto;
z-index: -1;
}
.lineBreak {
height: 16px;
}
.module {
border-left: 2px dotted red;
border-radius: 5px;
box-shadow: 0 0 5px -2px rgba(0,0,0,.4);
margin: auto;
height: 270px; width: 480px;
}
<body><!--All the comments you'll see below are meant to null 'white space' between layout elements--><!--
--><div class="backDrop" id="backDrop"><!--
--><div class="lineBreak"></div><!--
--><div class="module" id="module"></div>
</div>
Now for my question
It seems that my square function works perfectly, but printNum has some very strange behavior I need explained to me.
input ranging from 9 to 9999999 produces the number you'd expect.
input 99999999 produces 99999998.99999999
input 999999999 to 9999999999 produces the numbers you would expect.
input 999999999999 produces 99999999999.00002
input 9999999999999 to 999999999999999 produces the number you'd expect
and finally 9999999999999999 produces 10000000000000000
That last one I think I understand because it's sixteen digits, but I'd still like to get out of the realm of speculation.
Are there other numbers the produce strange results? What are they and why do they do it? Also how can I avoid e notation?