Javascript家庭作业大学。 [英] Javascript homework university.
本文介绍了Javascript家庭作业大学。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想比较阵列的图像,如果3张图像是草莓,那么应该有一个警告框,比如恭喜你,我怎么能这样做?
我尝试了什么:
I want to compare the images of the arrays, if 3 images are strawberry there should be an alert box like congrats you won how can I do that ?
What I have tried:
<html>
<body>
<script type="text/javascript">
var images = ["strawberry.jpg", "apple.jpg", "cherry.jpg", "orange.jpg", "pear.jpg"];
var length = images.length;
var randImg1 = Math.round(Math.random() * (length - 1));
document.write('<img src="' + images[randImg1] + '" >');
var randImg2 = Math.round(Math.random() * (length - 1));
document.write('<img src="' + images[randImg2] + '" >');
var randImg3 = Math.round(Math.random() * (length - 1));
document.write('<img src="' + images[randImg3] + '" >');
if (randImg1 == randImg2 && randImg2 == randImg3 && randImg3 == "strawberry.jpg") {
}
</script>
</body>
</html>
推荐答案
为什么要比较图像呢?这是很难确定相同的数字是否被选中3次。
由于每个图像与单个整数值直接相关,您只需比较整数值(randImg1,randImg2,randImg3)。图像与比较没有任何关系。它们只是所选值的直观表示。
您的代码检查所有3个值是否相互匹配,但您从未将它们与值进行比较0,这是草莓图像所代表的。
Why are you comparing images at all? That's the hard way to figure out if the same number was picked 3 times.
Since each image is directly related to a single integer value, you just compare the integer values (randImg1, randImg2, randImg3). The images will have nothing at all to do with the comparison. They are just a visual representation of the value that was picked.
Your code checks to see if all 3 values match each other, but you never compare them to the value 0, which is what is represented by the strawberry image.
试试这个
try this
if (randImg1 == randImg2 && randImg2 == randImg3 && images[randImg3] == "strawberry.jpg") {
alert('message')
}
这篇关于Javascript家庭作业大学。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文