Objective: Given a positive integer n
:
- If
n
is odd, output the list ofn
numbers closest to0
in increasing order - If
n
is even, output a Falsey value.
Test cases:
5 -> [-2,-1,0,1,2]
4 -> false (or any Falsey value)
1 -> [0]
Reference implementation
function update(){
var num = +document.getElementById("yield").value;
if(num){
var out = document.getElementById("output");
if(num % 2 == 1){
// base is balanced
var baseArr = [];
for(var i=0;i<num;i++){
baseArr.push(i-Math.floor(num/2));
}
out.innerHTML = baseArr.join(" ");
} else {
out.innerHTML = "false";
}
} else {
out.innerHTML = "<i>enter input</i>";
}
}
setInterval(update,1);
* {
font-family: "Constantia", "Consolas", monospace;
}
[type="number"] {
width: 10px;
width: 2em;
}
#output {
font-family: "Consolas", monospace;
}
Input: <input type="number" id="yield" value="3"> is <span id="output"><i>enter input</i></span>
[0]
as falsey result, even though \$n=1\$ results in[0]
as truthy result as well? \$\endgroup\$