从数组中拉出的列表缺少值

时间:2015-11-18 14:01:24

标签: javascript jquery arrays list random

当我点击我的计算按钮时,带有从数组中提取的值的新列表是可见的,但第二个数组缺少一个值。缺少的值并不总是相同的值。例如,如果我输入1,2,3,4,5,6,它并不总是丢失相同的数字,它可能是数组中的最后一个值,但我无法弄清楚出错的地方。这是代码:

$(document).ready(function() {
  function copyarray(arraytocopy) {
    var theCopy = []; // An new empty array
    for (var i = 0, len = arraytocopy.length; i < len; i++) {
      theCopy[i] = arraytocopy[i];
    }
    return theCopy;
  }

  function sortarray(arraytosort) {
    arraytosort.sort(
      function(a, b) {
        return Math.round(Math.random());
      }
    );
    return arraytosort;
  }

  function checkarrays(newarray, oldarray) {
    var swappositions = [];

    for (var i = 0; i < newarray.length; i++) {
      if (newarray[i] == oldarray[i]) {
        //alert(oldarray[i] + ' is the SAME!'); 
        swappositions.push(newarray[i]);
      }
    }

    var countsame = 0;
    swappositions.reverse();
    for (var i = 0; i < newarray.length; i++) {
      if (newarray[i] == oldarray[i]) {
        ///alert(oldarray[i] + ' is the SAME!');
        newarray[i] = swappositions[countsame];
        countsame = countsame + 1;
      }
    }

    for (var i = 0; i < newarray.length; i++) {
      if (newarray[i] == oldarray[i]) {
        //alert(oldarray[i] + ' is the SAME!'); 
        //swappositions.push(newarray[i]);
        var elementbefore = newarray[i - 1];
        newarray[i - 1] = newarray[i];
        newarray[i] = elementbefore;
      }
    }
    ///alert('test');
    ///alert('new array: ' + newarray);
    ///alert('old array: ' + oldarray);    
    //alert(swappositions.toString());
    //alert($.inArray( swappositions[0], newarray ));
    return true;
  }

  Array.prototype.randomize2 = function() {

    var oldarray = copyarray(this);

    sortarray(this);
    var notthesame = checkarrays(this, oldarray);

    if (notthesame = false) {
      //alert('sort again'); 
      sortarray(this);
      notthesame = checkarrays(this, oldarray);
    }


    //alert('new: '+this);
    //alert('old: '+oldarray);

    //alert('not the same!');
    return this;
  };


  function makelist(myarray) {

    var list = '<ol>';
    var listitem = '';

    for (var i = 0; i < myarray.length; i++) {

      if (/\S/.test(myarray[i])) {

        listitem = '<li>' + $.trim(myarray[i]) + '</li>';
        list += listitem;
      }

    }
    list += '</ol>';
    //alert(list.toString());
    return list.toString();

  }



  function combinelists(ordered, random) {

    var list = '<ol>';
    var listitem = '';

    for (var i = 0; i < ordered.length; i++) {

      if (/\S/.test(ordered[i])) {
        if ($.trim(random[i]) == $.trim(ordered[i])) {
          listitem = '<li class="same"><span class="yourname">' + $.trim(ordered[i]) + '</span> is matched with<span class="peersname">' + $.trim(random[i]) + '</span></li>';
          list += listitem;
        } else {
          listitem = '<li><span class="yourname">' + $.trim(ordered[i]) + '</span> is matched with <span class="peersname">' + $.trim(random[i]) + '</span></li>';
          list += listitem;
        }
      }

    }
    list += '</ol>';
    //alert(list.toString());
    return list.toString();

  }



  $('#ranGen').click(function() {
    //function randomize(){

    var lines = $('#names').val().split(/\n/);
    var texts = [];

    for (var i = 0; i < lines.length; i++) {
      // only push this line if it contains a non whitespace character.
      if (/\S/.test(lines[i])) {
        texts.push($.trim(lines[i]));
      }
    }




    var orderedlist = makelist(lines);
    //$( "#list" ).html(orderedlist);

    var linescopy = $.extend(true, [], lines);

    var randomarray = linescopy.randomize2();
    //alert(randomarray);

    var randomlist = makelist(randomarray);
    //$( "#randomlist" ).html(randomlist);


    var combinedlists = combinelists(lines, randomarray);
    $("#combined").html(combinedlists);


  });
});
textarea {
  height: 100px;
  width: 400px;
}
input {
  display: block;
}
.list {
  display: inline-block;
}
.same {
  color: orange;
}
.yourname {
  color: blue;
}
.peersname {
  color: brown;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>Get a random match </h1>
<p>Insert all your names into the textarea below. Each name should be on a new line. Hit the button to generate your matches.</p>
<form>
  <textarea id="names" placeholder="Enter list of names here. Each name should be on a new line."></textarea>
  <input type="button" id="ranGen" value="Go" onClick="JavaScript:randomize()" />
</form>

<div id="list" class="list"></div>
<div id="randomlist" class="list"></div>
<div id="combined" class="list"></div>

1 个答案:

答案 0 :(得分:2)

过滤掉#34;空&#34;行应解决问题

var lines = $('#names').val().split(/\n/).filter(function (val) {
    return val.trim() != '';
});