Tujuan : Diberikan bilangan bulat positif n
:
- Jika
n
ganjil, keluarkan daftarn
angka yang paling dekat0
dalam urutan meningkat - Jika
n
genap, hasilkan nilai Falsey.
Kasus uji :
5 -> [-2,-1,0,1,2]
4 -> false (or any Falsey value)
1 -> [0]
Implementasi referensi
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>