這是我的問題,希望得到一些支持。創建表單功能Html
這是我的功能。
function show(n,q)
{
for(j=1;j<=n;j++)
{
s=j.toString().length;
t=0;
for(i=s-1;i>=0;i--)
{
t+=Math.pow((Math.floor(j/Math.pow(10,i))%10),q);
}
if(t==j){document.write(t+ " ");}
else{document.write("");}
}
}
show(1000,3);
隨着兩個輸入:編號Ñ和指數q它將解決所有大於n,所有的q exponented的其數字的總和等於本身更小的數字。 例如:q = 3,n = 200,因爲:1^3 + 5^3 + 3^3 = 153
此功能正常,但由於我的JavaScript技能不佳,不知道如何創建一個窗體,可以輸入n和q爲2個框,然後單擊按鈕「顯示」我們在第三個框中顯示結果。
我曾經嘗試這樣做下面的代碼,但它不工作:(
<input id='number' type='text' />
<input id='exp' type='text' />
<button onclick='javascript:show()'>Show</button>
<div id='res' style='width:100%;height:200px'></div>
<script>
function show() {
var n=document.getElementById('number').value,
var q=document.getElementById('exp').value,
out=document.getElementById('res'),
out.innerHTML="";
for(j=1;j<=n;j++)
{
s=j.toString().length;
t=0;
for(i=s-1;i>=0;i--)
{
t+=Math.pow((Math.floor(j/Math.pow(10,i))%10),q);
}
if(t==j){
out.innerHTML+=t+ " ";
}
else{
out.innerHTML+="";
}
}
}
</script>
在additon,我想這樣做我自己,可能你們告訴我在哪裏可以爲這個問題找到指南。
感謝
的代碼看起來不錯,並會做你正在嘗試做的。只是有一些,(逗號)而不是; (分號)在你的代碼中。改變它們然後嘗試。 – Ankit