1

我正在做一个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>
4

1 回答 1

0
<html>

<head>


<title> random text </title>

<script type="text/javascript"> 

window.onload=display;

//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 arr=new Array(10,1);
x=0;
y=0;
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];
    arr[x,0]=colours[choice];
    arr[x,1]=quotations[a];
    x=x+1;
    console.log(arr);

  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(this.id);display()" id="red"><font color="red">Red </font></button>

<button onClick="compare(this.id);display()" id="yellow"><font color="yellow">Yellow</font></button>

<button onClick="compare(this.id);display()" id="green"><font color="green">Green</font></button>

<button onClick="compare(this.id);display()" id="blue"><font color="blue">Blue</font></button>



</body>

</html>
于 2012-11-18T05:40:09.620 回答