在mousedownevent上从1-100生成10个随机整数时遇到问题

Having trouble generating 10 random integers from 1-100 on a mousedownevent

本文关键字:整数 遇到 问题 随机 10个 mousedownevent 上从 1-100生成      更新时间:2023-09-26

我在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 show数组是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 with 10 and innerHTML fixed

生成从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); 

如果您NOT使用jQuery,您可以将此代码与Xotic 提供的混合

在mousedown上实现我使用了点击,但如果你真的需要鼠标放下,可以随意更改。

将上面的代码包装成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');
});

多亏了tryingToGetProgrammingTraight的输入,我发现输出不需要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;

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++) {

不管怎样,你想要什么,得到什么?请澄清。