Javascript 如何为每个正确答案制作一个简单的图表
明天我要参加一个大考,我不太确定每次你得到正确答案时如何绘制图表,也不知道如何找到/寻找一个好的解决方案。简单地说,每次我回答正确时,我都需要一个cavas在柱/柱上添加x高度,使用JAVASCRIPT 如果你能告诉我如何做到这一点,同时在这个网站上使用的代码:这将是惊人的 编辑: 柱/柱可能是这样的:(不确定) 编辑2:Javascript 如何为每个正确答案制作一个简单的图表,javascript,html,canvas,Javascript,Html,Canvas,明天我要参加一个大考,我不太确定每次你得到正确答案时如何绘制图表,也不知道如何找到/寻找一个好的解决方案。简单地说,每次我回答正确时,我都需要一个cavas在柱/柱上添加x高度,使用JAVASCRIPT 如果你能告诉我如何做到这一点,同时在这个网站上使用的代码:这将是惊人的 编辑: 柱/柱可能是这样的:(不确定) 编辑2: <script> var score = 0; var questions = [ ['How many moons does Earth have?',
<script>
var score = 0;
var questions = [
['How many moons does Earth have?', 1],
['How many moons does Saturn have?',31],
['How many moons does Venus have?', 0]
];
for (var i=0; i<questions.length; i++) {
askQuestion(questions[i]);
}
function askQuestion(question) {
var answer = prompt(question[0],'');
if (answer == question[1]) {
alert('Correct!');
score++;
} else {
alert('Sorry. The correct answer is ' + question[1]);
}
}
</script>
</head>
<body>
<script>
var message = 'You got ' + score;
message += ' out of ' + questions.length;
message += ' questions correct.';
document.write('<p>' + message + '</p>');
</script>
<canvas id="mycanvas"> </canvas>
</body>
</html>
var得分=0;
变量问题=[
[“地球有多少颗卫星?”,1],
[“土星有多少颗卫星?”,31],
[“金星有多少颗卫星?”,0]
];
对于(var i=0;i,这里有一些代码可以帮助您开始
检查此代码并将其应用于问题中的链接
然后开始学习准备考试
给定一个数组,其中包含每个问题的正确答案数
// an array that holds the count of correct responses for each answer
var answers=[];
for(var i=0;i<answers.length;i++){
// how many correct answers for this question
correctCount=answers[i];
// make each bar 20 pixels apart on the x-axis
answerX=i*20;
// all bars grow from the same bottom line on the y-axis
answerY=150;
// all bars are the same width
answerWidth=10;
// each bar is 10 pixels taller for each correct answer
answerHeight=correctCount*10;
// draw this bar
// a negative answerHeight will cause the bar to draw
// upward instead of the usual downward
ctx.fillRect(answerX,answerY,answerWidth,-answerHeight);
}
//一个数组,用于保存每个答案的正确响应计数
var回答=[];
对于(var i=0;我无法使它正常工作,如果您能看看edit2,它对我来说是一个更容易理解和学习的脚本,那么您可以演示给我看吗?正如您所料,我不想为您做这项工作;-)你的测验链接有必要的代码询问你的问题。结合我上面给出的条形图代码。完成后,你应该有足够的经验在考试中得分。祝你明天好运!
// an array that holds the count of correct responses for each answer
var answers=[];
for(var i=0;i<answers.length;i++){
// how many correct answers for this question
correctCount=answers[i];
// make each bar 20 pixels apart on the x-axis
answerX=i*20;
// all bars grow from the same bottom line on the y-axis
answerY=150;
// all bars are the same width
answerWidth=10;
// each bar is 10 pixels taller for each correct answer
answerHeight=correctCount*10;
// draw this bar
// a negative answerHeight will cause the bar to draw
// upward instead of the usual downward
ctx.fillRect(answerX,answerY,answerWidth,-answerHeight);
}