JavaScript显示随机图像

JavaScript显示随机图像,javascript,image,random,Javascript,Image,Random,冒着人们对我不满意的风险,我将发布我已经拥有的代码。请看评论,让我知道我错在哪里 是的,这是家庭作业,是的,我看了视频,看了我们的书JavaScript by Example,顺便说一句,这是一本可怕的书。我试着给我的老师发电子邮件,但什么也没得到。这是一个为期5周的JavaScript课程介绍,我显然一点也不理解 //创建一个名为imagesArray的数组,其中包含七个图像文件名:dog.jpg、fox.jpg、mouse.jpg、alligator.jpg、fish.jpg、parrot

冒着人们对我不满意的风险,我将发布我已经拥有的代码。请看评论,让我知道我错在哪里

是的,这是家庭作业,是的,我看了视频,看了我们的书JavaScript by Example,顺便说一句,这是一本可怕的书。我试着给我的老师发电子邮件,但什么也没得到。这是一个为期5周的JavaScript课程介绍,我显然一点也不理解

//创建一个名为imagesArray的数组,其中包含七个图像文件名:dog.jpg、fox.jpg、mouse.jpg、alligator.jpg、fish.jpg、parrot.jpg和cat.jpg imagesArray=新阵列7; imagesArray[0]=新图像; imagesArray[0].src=new dog.jpg; imagesArray[1]=新图像; imagesArray[1].src=new fox.jpg; imagesArray[2]=新图像; imagesArray[2].src=new mouse.jpg; imagesArray[3]=新图像; imagesArray[3].src=new alligator.jpg; imagesArray[4]=新图像; imagesArray[4].src=newfish.jpg; imagesArray[5]=新图像; imagesArray[5].src=new parrot.jpg; imagesArray[6]=新图像; imagesArray[6].src=new cat.jpg; 函数显示图像{ var num=Math.floorMath.random; document.getElementByIdimagesArray[num]; } //创建一个名为displayImage的函数 //它不应该向其中传递任何值 //displayImage的语句块应该有两条语句 //第一条语句应该生成一个范围为0到6的随机数,即imagesArray中图像文件名的下标值 //第二条语句使用随机数作为下标值,在画布图像中显示imagesArray数组中的随机图像 //生成随机数时,可能需要使用以下公式 //随机数*imagesArray提示中的图像数使用适当的数学方法生成随机数 //记住数组的下标值是0到6个七元素的零基数组 //您必须从生成的随机数中减去1,以说明基于零的数组 //在下面的按钮标记中,添加一个调用displayImage函数的onClick事件处理程序 //不要向displayImage函数传递任何值
你很接近!只是错过了几件事

您的随机数生成器将生成一个介于0-1之间的数字。要使其生成一个介于0-5 1-6之间的数字,但请记住减去一,因为这是数组的索引,请使用以下代码段:

var num = Math.floor(Math.random() * 5);
您需要向按钮元素添加onClick属性

<input type="button" onClick="displayImage()" value="Display Random Image">
HTML:

如果你想让它变得更好A+;使用:


谢谢你的帖子!我是这样申请的

JS

HTML


查看在javascript控制台中执行Math.floorMath.random时得到的结果。它将始终为0。这是因为Math.random始终返回0到1之间的十进制值,而Math.floor在0到1之间的十进制值上始终为0。这有用吗?你被困在哪里了?这些评论准确地告诉你该做什么,而且,虽然我理解学习新东西并不容易,但这些个人需求中的大多数都可以通过简单地使用谷歌来解决。我确实尝试过谷歌。将var num=Math.floorMath.random*7;工作得更好?你说的JavaScript控制台是什么意思?我在Sublime中输入所有内容,然后保存它,然后在firefox中打开浏览器。现在您可以开始了:下一步是查找document.getElementById的文档。它只是从页面返回一个元素——现在你必须处理它返回的内容。当你在web浏览器中打开开发者工具时,你会得到一个Javascript控制台。在Chrome中称为开发者工具,在Firefox中称为Web控制台。您可以在控制台中输入任何javascript代码并立即查看结果。您甚至使用了placehold。它。。。尼斯:这就是为什么我如此困惑的原因。他根本不是一个好老师。@EllenHarris我重新阅读了他写的关于减法1…的帮助说明,这也让我感到困惑祝你好运,不要害怕JS。毕竟,正如你所看到的,这很有趣!我会确保我下一节课是亲自来的。你是对的,JavaScript让我非常感兴趣,希望它会像HTML一样开始吸引我。我还想让你知道,我的另外两个同学也感谢你。是的,3个初学者无法理解:@EllenHarris如果有一天你想用HTML5、CSS3建立网站或网络应用程序,没有他们的弟弟Javascript你就做不了什么。
<!-- 
    //In the button tag below add an onClick event handler that calls the displayImage function
    //do not pass any value to the displayImage function
-->

<form name="imageForm">
  <table border=3>
  <tr>
    <td>
      <input onclick="displayImage();" type=button value="Display Random Image">
    </td>
  </tr>
  <tr>
    <td>
      <img src="blank.jpg" name="canvas" />
    </td>
  </tr>
  </table>
</form>
//create an array named imagesArray that contains the seven image file names
//dog.jpg, fox.jpg, mouse.jpg, alligator.jpg, fish.jpg, parrot.jpg and cat.jpg

var imagesArray = ["dog.jpg", "fox.jpg", "mouse.jpg", "alligator.jpg", "fish.jpg", "parrot.jpg", "cat.jpg"];


//create a function named displayImage
//it should not have any values passed into it

function displayImage(){

    //the first statement should generate a random number in the range 0 to 6 (the subscript values of the image file names in the imagesArray)
    var num = Math.floor(Math.random() * 7); // 0...6
    //the second statement display the random image from the imagesArray array in the canvas image using the random number as the subscript value
    document.canvas.src = imagesArray[num];

}

//remember the subscript values of the array are 0 to 6 (seven elements) zero based array
//you will have to subtract 1 from the random number generated to account for the zero based array
var num = Math.floor(Math.random() * (imagesArray.length+1)); // 0...6
var imgArr = ["img.jpg","name.jpg","could.jpg","be.jpg","anything.jpg"];

function displayImg(){
    var num = Math.floor(Math.random() * (imgArr.length));
    document.canvas.src="img/"+imgArr[num];
}
<input type="button" onClick="displayImg()" value="Display Random Image">
<img src="img/dog4.jpg" name="canvas" style="width:200px"/>