Javascript 使用if语句获取输入的百分比?
一切正常,但我想补充几点。在函数的最后,我想通过比较你想回答的数量中有多少是正确的,但是有一个百分比,来改变画布的高度。很抱歉,这很难理解 JavaScript:Javascript 使用if语句获取输入的百分比?,javascript,html,Javascript,Html,一切正常,但我想补充几点。在函数的最后,我想通过比较你想回答的数量中有多少是正确的,但是有一个百分比,来改变画布的高度。很抱歉,这很难理解 JavaScript: <script type="text/javascript"> function myFunction() { score = 0; questionAmount = prompt('How many question would you like to solve?'); for(i = 0; i < que
<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>
函数myFunction(){
得分=0;
questionAmount=提示(“您希望解决多少个问题?”);
对于(i=0;i<0;i++){
var x=Math.floor(Math.random()*13);
var y=Math.floor(Math.random()*13);
问题=提示(“什么是“+x+”*“+y+”=?”);
如果(问题==null | | isNaN(问题)){
打破
}
如果(问题==x*y){
分数++;
}
}
警惕('你得到了'+score+'出'+questionAmount+'正确');
}
HTML:
点击
只需创建百分比并应用它:
var correctPercent = (parseDouble(score) / parseDouble(questionAmount)) * 100;
//change the target elements style, apply above variable
document.getElementById("content").style.height = correctPercent + "%";
所以当用户有更多的问题时,你想要一些更大的条,对吗?你面临的问题是什么?是的,凯文,这个例子和我想要的非常相似。这不会产生一个百分比值,它只是一个分数?顺便说一句,至少
score
不需要解析dedite@Bergi——我们得到了there@Bergimul乘以100以获得适当的百分比,但数学是正确的
var correctPercent = (parseDouble(score) / parseDouble(questionAmount)) * 100;
//change the target elements style, apply above variable
document.getElementById("content").style.height = correctPercent + "%";