10
2
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>
Can the output be a range object rather than a list? – Brad Gilbert b2gills – 2015-11-08T20:40:36.283
@BradGilbertb2gills Sorry, a range object is an invalid output. – Conor O'Brien – 2015-11-08T20:42:41.160
The empty list is not always falsey. – SuperJedi224 – 2015-11-08T21:44:20.413
@SuperJedi224 In which contexts? – Conor O'Brien – 2015-11-08T21:45:04.147
There are languages (I.E. Javascript) in which the empty list is considered a truthy value. – SuperJedi224 – 2015-11-08T21:46:07.403
@SuperJedi224 so it is... – Conor O'Brien – 2015-11-08T21:46:35.690