我正在做一个javascript网页来演示stroop效果,即如果单词的颜色与其语义不匹配,那么一个人选择正确的颜色需要更长的时间。例如,如果单词 RED 为蓝色,则单击“蓝色”按钮。
但是,由于颜色和单词是随机生成的,我不知道如何将它们存储到新数组中并进行比较。
在实验结束时,我想要一个警报。如果问题是“RED in blue”,那么它会显示“DIFF”,如果问题是“RED in red”,那么它会显示 10 个问题的“SAME”。感谢帮助!我有如下代码
<html>
<head>
<title> random text </title>
<script type="text/javascript">
//array
var quotations = new Array();
quotations[0]= "red";
quotations[1]= "yellow";
quotations[2]= "green";
quotations[3]= "blue";
var colours = new Array();
colours[0] = "red";
colours[1] = "yellow";
colours[2] = "green";
colours[3] = "blue";
//Randomize coloured text 9 times and on tenth time, display alert
var NumberAttempts = 0;
var choice;
var score =0;
function display()
{
if ( NumberAttempts < 10)
{
var a = Math.floor(Math.random()*quotations.length)document.getElementById ("txtbox").value=quotations[a];
choice = Math.floor(Math.random()*colours.length);document.getElementById ("txtbox").style.color=colours[choice];
NumberAttempts++;
}
else
{
alert ("You are done! The number of correct attempts is " + score + ".");
}
}
function compare(c1)
{
if (colours[choice]==c1)
{
score++;
}
}
</script>
</head>
<body>
<h1><center>Using Computer Science to Demonstrate the Stroop Effect</center></h1>
<div>
<ul>
<li><a href="http://introduction.html" target="_blank"> Introduction </a></li>
<li><a href="http://instructions.html" target="_blank"> Instructions </a></li>
</ul>
<textarea id="txtbox" style="width:600px;"></textarea><br />
<button onClick='compare("red");display()'><font color="red">Red </font></button>
<button onClick='compare("yellow");display()'><font color="yellow">Yellow</font></button>
<button onClick='compare("green");display()'><font color="green">Green</font></button>
<button onClick='compare("blue");display()'><font color="blue">Blue</font></button>
</body>
</html>