JavaScript代码帮助-错误的返回 [英] Javascript code help - Wrong return

查看:68
本文介绍了JavaScript代码帮助-错误的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究这个脚本.完成后,应将其用于创建2和2组.但不管怎么说;脚本开始处的输入"数组将从我的HTML文件中获得22种不同的输入.作为标准,我给他们值1-22.问题是我的两个块第一个数字"和第二个数字"不能很好地工作:它们没有返回正确的数字.因为我希望每个elev [x]都使用一次!不是2次,不是0次,一次!并且这些块返回两次,其中一些甚至不使用.那我该如何解决呢?

So I'm working on this script. When I'm done with it, it should be used for making 2-and-2 groups. But anyway; The 'input' array in the start of the script, will get 22 different inputs from my HTML file. As a standard, I gave them the values 1-22. The thing is my two blocks '1st number' and '2nd number' doesn't work very well: they don't return the right numbers. Cause I want every elev[x] to be used once! Not 2 times, not 0 times, once! And the blocks returns like some of them twice and some of them isn't even used. So how can I fix this?

    function Calculate(){
    var elev = [];
    var inputs = document.getElementsByName("txt");
    for(i=0; i<inputs.length; i++) {
    elev[i] = {
        "Value": inputs[i].value,
        "Used": false
    };
}

    function shuffle(elev){
    var len = elev.length;
    for(i=1; i<len; i++){
        j = ~~(Math.random()*(i+1)); 
        var temp = elev[i];
        arr[i] = elev[j];
        arr[j] = temp;
        }
    }

    for(var v=0; v<1; v++) {
        shuffle(elev);
        document.write(elev + '<br/>\n');
    }}

是的,我还是编程新手,我只是想学习我能学到的东西.

Yes, I'm still new at programming and I just wanna learn what I can learn.

通过执行Fisher-Yates随机播放来解决问题.

推荐答案

将数组改组并对其进行迭代的想法是正确的,但是,通过抛硬币比较器进行排序(常见的错误实现;另一种建议) (答案)不是不是随机排列数组的正确方法;它会产生非常偏斜的结果,甚至不能保证完成.

The idea of shuffling an array and iterating over it is correct, however, sorting by a coin-flipping comparator (a common mis-implementation; suggested by the other answer) is not the correct way to shuffle an array; it produces very skewed results and is not even guaranteed to finish.

除非您愿意获取underscore.js(arr = _.shuffle(arr)),否则建议使用Fisher-Yates随机播放:

Unless you're willing to fetch underscore.js (arr = _.shuffle(arr)), It is recommended to use the Fisher-Yates shuffle:

function shuffle(arr){
  var len = arr.length;
  for(var i=1; i<len; i++){
    var j = ~~(Math.random()*(i+1)); // ~~ = cast to integer
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}

...

shuffle(elev);
for(var i=0; i<elev.length; i++){
  //do something with elev[i]
  console.log(elev[i].Value);
}

另外,请注意对象字段应该是小写字母,而不是大写字母(value', not 'Value'). Also, the Used`字段不是必须的,因为您现在按顺序进行迭代.而且,您有可能使用所有值数组吗?它们似乎是您对象中的唯一字段.

also, note that object fields should be lowercase, not uppercase (value', not 'Value'). Also, theUsed` field should not be neccessary since you now iterate in order. Also, any chance you could use an array of Values? They seem to be the only field in your objects.

此外,不要使用document.write().在页面加载后使用时,它无法按预期工作.如果您想向文档中添加某些内容,则讨厌jQuery和其他框架,并且不想花很多时间来创建和组合DOM节点,document.body.innerHTML +=仍然比document.write()更好(但仍然考虑添加DocumentFragment s代替").

Also, Don't use document.write(). It doesn't work as expected when used after the page load. If you want to append something to the document, you hate jQuery and other frameworks and you don't want to go the long way of creating and composing DOM nodes, document.body.innerHTML += is still better than document.write() (but still consider appending DocumentFragments instead').

演示: http://jsfiddle.net/honnza/2GSDd/1/

这篇关于JavaScript代码帮助-错误的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆