0

人々が再び私に満足しない危険を冒して、私はすでに持っているコードを投稿します. コメントを見て、どこが間違っているか教えてください。

はい、これは宿題です。はい、ビデオを見たり、私たちの本 (JavaScript by Example) を見たりしましたが、これは恐ろしい本です。先生にメールしてみましたが、何も返ってきません。これは JavaScript クラスの 5 週間の紹介であり、明らかに何も理解していません。

// 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
imagesArray = new Array(7);
imagesArray[0] = new Image();
imagesArray[0].src = new "dog.jpg";
imagesArray[1] = new Image();
imagesArray[1].src = new "fox.jpg";
imagesArray[2] = new Image();
imagesArray[2].src = new "mouse.jpg";
imagesArray[3] = new Image();
imagesArray[3].src = new "alligator.jpg";
imagesArray[4] = new Image();
imagesArray[4].src = new "fish.jpg";
imagesArray[5] = new Image();
imagesArray[5].src = new "parrot.jpg";
imagesArray[6] = new Image();
imagesArray[6].src = new "cat.jpg";

function displayImage() {
  var num = Math.floor(Math.random());
  document.getElementById(imagesArray[num]);
}

// create a function named displayImage
// it should not have any values passed into it
// the statement block of the displayImage should have two statements
// 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)
// the second statement display the random image from the imagesArray array in the canvas image using the random number as the subscript value
// when you generate the random number you might want to use the following formula
// a random number * the number of images in the imagesArray (Hint use the appropriate Math method to generate a random number
// 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
// 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 type=button value="Display Random Image">
      </td>
    </tr>
    <tr>
      <td>
        <img src="blank.jpg" name="canvas">
      </td>
    </tr>
  </table>
</form>

4

3 に答える 3

5

ライブデモ

HTML:

<!-- 
    //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>

JS:

//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

さらに良くしたい場合 ( A+ ;) ) を使用します。

var num = Math.floor(Math.random() * (imagesArray.length+1)); // 0...6
于 2013-10-30T20:51:45.720 に答える
2

あなたはとても近いです!いくつか欠けているだけです。

乱数ジェネレーターは 0 ~ 1 の数値を生成します。0 から 5 の間の数値 (1 から 6 ですが、これは配列のインデックスであるため、1 を引くことを忘れないでください) を生成するには、次のスニペットを使用します。

var num = Math.floor(Math.random() * 5);

ボタン要素に onClick 属性を追加する必要があります。

<input type="button" onClick="displayImage()" value="Display Random Image">
于 2013-10-30T20:44:28.487 に答える