When I hit my calculate button, the new lists with values pulled from the arrays are visible, but the second array is missing a value.The missing value is not always the same value. For example if I put in 1,2,3,4,5,6, it's not always the same number missing, it's probably the last value in the array, but I can't figure out where I went wrong. Here's the code:
$(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="http://ift.tt/1qRgvOJ"></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 student 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>
Aucun commentaire:
Enregistrer un commentaire