<script> function fn(num) { var obj = {}; var a = num/2|0, b = num - a; obj[a] = a > 1 ? fn(a) : a; obj[b+' '] = b > 1 ? fn(b) : b; return obj } console.log(fn(9)) document.write(JSON.stringify(fn(9),null,1)) </script>