使用if语句获取输入的百分比?

时间:2013-12-20 14:46:07

标签: javascript html5

一切都正常运行,但我想添加一些东西。在函数的最后,我想通过比较你想要回答的数量,但是用百分比来改变画布的高度。对不起,如果这很难理解。

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>

1 个答案:

答案 0 :(得分:1)

只需创建百分比并应用它:

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

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