I'm currently populating before
and after
arrays with indexes, based on a number provided. If the input going in is an int, there will be three indexes (left, middle and right). If it is a decimal, there will just be two (left, right). There will me a maximum of three indexes to sort into a maximum of two arrays (before and after). To understand, here is some expected input/output:
input: 1
output: before [] after [0, 1, 2]
input: 1.5
output: before [] after [1, 2]
input: -3.4
output: before [3, 4] after []
input: 0
output: before [1] after [0, 1]
Essentially, the input index gets floored and ceiling'd (or, in the case of a whole number, their next/previous integers are used along with the original input). If any of the resulting integers are below 0, they get put in the before
array, but their index is made absolute, otherwise they go into the after
array. If they do get put into the before
array, then they are put in to reverse order, such that they remain sorted in numerical order.
I have the below working code. But I feel like I could do much better. How would the community go about optimising this?
var input = document.getElementById('input');
var button = document.getElementById('button');
var ouput = document.getElementById('output');
function getOutput(input) {
var left = Math.floor(input);
var right = Math.ceil(input);
var middle;
var before = [];
var after = [];
if (right == left)
left--,
middle = left + 1,
right = left + 2;
if (left < 0)
before.unshift(Math.abs(left));
else
after.push(left);
if (middle < 0)
before.unshift(Math.abs(middle));
else if (typeof middle == 'number')
after.push(middle);
if (right < 0)
before.unshift(Math.abs(right));
else
after.push(right);
return {before: before, after: after};
}
function buttonPressed () {
var i = parseFloat(input.value);
var msg = 'not a number';
if (!isNaN(i)) {
var o = getOutput(i);
msg = 'before [' + o.before.toString() + '] ahead [' + o.after.toString() + ']';
}
ouput.innerHTML = msg;
}
<input id="input" type="text">
<input id="button" type="button" value="get" onclick="javascript:buttonPressed();">
<br><br>
<div id="output" style="font-family: monospace;"></div>
My interest is in the getOutput
method. The other stuff is for demonstration purposes.
Update
I have adjusted the expected output and the code from the original question, after being prompted to rethink from the comments.
before
should be[-3, -2]
but the code produces[-4, -3]
. Which is the intended result? \$\endgroup\$-n
, but doing this is not possible with JavaScript. Not if you want to use the Array prototype properly. Perhaps there is a better way to do this, I'm all ears. :-) \$\endgroup\$