2013-12-20 26 views
0

一切正常運行,但我想添加一些東西。在函數的最後,我想通過比較你想要回答的數量,但是用一個百分比來改變畫布的高度。很抱歉,如果這很難理解。使用if語句來獲得輸入的百分比?

的JavaScript:

<script type="text/javascript"> 
function myFunction() { 

score = 0; 

questionAmount = prompt('How many question would you like to solve?'); 

for(i = 0; i < questionAmount; i++) { 
    var x = Math.floor(Math.random() * 13); 
    var y = Math.floor(Math.random() * 13); 

    question = prompt('What is ' + x + ' * ' + y + ' = ?'); 

    if(question == null || isNaN(question)) { 
     break; 
    } 
    if(question == x * y) { 
     score++; 
    } 
} 

alert('You got ' + score + ' out of ' + questionAmount + ' correct.'); 
} 
</script> 

HTML:

<canvas id="content"></canvas> 
<br /> 
<button onclick="myFunction()">CLICK</button> 
+0

所以,你想一些酒吧,是更大的,當用戶有更多的問題正確? [類似這樣的東西?](http://www.asp.net/ajaxlibrary/GetFile.aspx?Page=jquery_ui_mvc_progressbar&File=basic.PNG) – Kevin

+1

你遇到了什麼問題? – Bergi

+0

是的,凱文,這個例子與我想要的非常相似。 – user2793503

回答

1

剛剛創建的百分比,並將其應用於:

var correctPercent = (parseDouble(score)/parseDouble(questionAmount)) * 100; 

//change the target elements style, apply above variable 
document.getElementById("content").style.height = correctPercent + "%"; 
+0

這不會產生百分比值,它只是一個分數?順便說一句,至少'分數'不需要被解析 – Bergi

+0

編輯@Bergi - 到達那裏 – tymeJV

+0

@Bergi多爾100得到一個適當的百分比,但數學是正確的 – Kevin