1

mousedownevent で 1 から 100 までの 10 個のランダムな整数を生成するのに問題があります。また、それぞれを表の行に表示する必要があります。私はコンピュータープログラミングの初心者であり、犯している間違いが何であるかわかりません。

これが私のコードです:

function process() {
    'use strict';

    var ara = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var index;
    var output = '<tr>';

    for (var i = 0; i < 1 0; i++) {
        index = Math.floor(Math.random() * 100);
        output += '<td>' + ara[index] + '</td>';
    }

    output += '</tr>';
    document.getElementById('numbers').innerHtML = output;
}

function init() {
    'use strict';

    document.getElementById('showarray').onmousedown = process;
} // End of init() function

window.onload = init;

ID番号はテーブルタグで、IDショー配列はH3タグで、10個の整数を取得するためにクリックする必要があります

ここにHTMLがあります

<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <title>assignment2.html</title>
        <!--[if lt IE 9]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <link rel="stylesheet" href="css/styles.css">
    </head>

    <body>
        <!-- assignment2.html -->
        <h2>10 Random Integers from 1 to 100</h2>

        <h3 id='showarray'>Mouse down here to show integers in table below</h3>

        <table id="numbers" border="2"></table>
        <span id="show5th">The fifth element is ?</span>

        <script src="js/assignment2.js"></script>
    </body>

</html>

jsfiddleと修正10_innerHTML

4

3 に答える 3

0

TryingToGetProgrammingStraight の入力のおかげで、出力に ara が必要ないことがわかりました。1 から 100 までの乱数を生成するには、インデックスだけで十分でした。

function process() {
    'use strict';

    var ara = [1, 2, 3, 4, 5, 6, , 7, 8, 9, 10];
    var index;
    var output = '<tr>';

    //Start of loop
    for (var i = 0; i < 10; i++) {
        index = Math.floor(Math.random() * 101);
        output += '<td>' + +index + '</td>';

    }

    output += '</tr>';
    document.getElementById('numbers').innerHTML = output;
}

function init() {
    'use strict';

    document.getElementById('showarray').onmousedown = process;
} // End of init() function

window.onload = init;
于 2013-09-02T03:02:33.707 に答える
0

1 から 100 までの 10 個の乱数を生成します。

var numbers = [];
while (numbers.length <10) {
    // ParseInt for rounding. Real random numbers are 99 starting on 1.
    var number = parseInt(Math.random() * 99)+1,10); 
    // save the number into an array and avoid duplicated.
    if (numbers.indexOf(number) === -1 ) {
        numbers.push(number);
    }
}

表のセルに数値を表示する

jQuery を使用している場合、これは簡単です。

// creates table row element
var row = $('<tr>');
$.each(numbers, function(i) {
    // creates new table cell element
    var cell = $('<td>'); 
    cell.text(i);
    // append cell into row without inserting into the DOM.
    cell.append(row);
});    
// appends the resulting row and it's content into the DOM element with an id of `#target`
$('#target').append(row); 

jQueryを使用していない場合は、このコードを Xotic が提供する

マウスダウンでそれを実現するために クリックを使用しましたが、本当にマウスダウンが必要な場合は、好きなように変更してください。

上記のコードを 2 つの関数にラップします。

var generateRandomNumbers = function () {
    var numbers = [];
    while (numbers.length <10) {
        var number = Math.ceil(Math.random() * 100);
        if (numbers.indexOf(number) === -1 ) {
            numbers.push(number);
        }
    }
    return numbers;
}

var appendNumbersToDom(numbers, target) {
    // you may want to check for non valid paramenters here
    var row = $('<tr>';
    $.each(numbers, function(i) {
        var cell = $('<td>');
        cell.text(i);
        cell.append(row);
    });    
    $(target).append(row);    }

と発信者

$('#showarray').on('click', function() {
    appendNumbersToDom(generateRandomNumbers, '#target');
});
于 2013-09-01T05:53:00.440 に答える
-1

for(var i= 0; i < 1 0; i++) {for(var i= 0; i < 10; i++) {1と0の間にスペースがないはずです

また、もっと重要なara[index]ことindexは、なぜあなたは持っているのaraですか?ループの場合

  1. その不必要
  2. 次に、for(var i in ara)代わりに使用しますfor(var i= 0; i < 10; i++) {

とにかく、あなたは何を望み、何を得ていますか?どうか明らかにしてください。

于 2013-09-01T05:37:17.123 に答える