我试图在除法运算的每次迭代中增加填充,每个除法迭代结果必须向右移位。这是我的代码:
<script>
function test(n,m){
var x = "";
var y = n/m;
var z = Math.floor(y)*m;
var p = n%m;
var t = document.querySelector("#demo");
t.style.paddingLeft = "5%";
if(n<m)
{
return "";
}
else
{
x = m +"|"+n+"|"+Math.floor(y)+"<br>"+"   "+z+" <br>"+"  "+"___"+"<br>"+"    "+p;
return (x +"<br>"+ test(Math.floor(y),m));
}
}
</script>
Enter The Divident: <input type="text" id="num" size="20"> <br> <br>
Enter The Divisior: <input type="text" id="numb" size="20"><br>
<button onclick="document.getElementById('demo').innerHTML= test(parseInt(document.getElementById('num').value),parseInt(document.getElementB yId('numb').value));">Try it</button> <br> <br>
<div id="demo" style="border:0px solid black; margin-left:0px">
</div>
答案 0 :(得分:0)
将您的递归输出包装在div中,只需使用CSS添加填充,就像这样
<强> CSS 强>
<style>
#demo div {
padding-left: 5%;
}
</style>
(略有)修改后的代码
<script>
function test(n, m) {
var x = "";
var y = n / m;
var z = Math.floor(y) * m;
var p = n % m;
var t = document.querySelector("#demo");
if (n < m) {
return "";
}
else {
x = m + "|" + n + "|" + Math.floor(y) + "<br>" + "   " + z + " <br>" + "  " + "___" + "<br>" + "    " + p;
return (x + "<div>" + test(Math.floor(y), m) + "</div>");
}
}
</script>
Enter The Divident: <input type="text" id="num" size="20"> <br> <br>
Enter The Divisior: <input type="text" id="numb" size="20"><br>
<button onclick="document.getElementById('demo').innerHTML=test(parseInt(document.getElementById('num').value),parseInt(document.getElementById('numb').value));">Try it</button> <br> <br>
<div id="demo" style="border:0px solid black; margin-left:0px"></div>