Home > database >  Javascript for card shuffle failing
Javascript for card shuffle failing

Time:12-05

Over my head with javascript. I'm trying to get the cards to shuffle when clicking next.

Currently, it moves forward with one random shuffle and stops. Back and forward buttons then no longer work at that point.

Please help—many thanks.

When I'm lost and unsure what sample of the code to pinpoint, the captions go up to 499. The sample is also here: https://rrrhhhhhhhhh.github.io/sentences/

Very new to javascript. So any help is greatly appreciated. Very open to better ways to achieve this???

How I have it set up: HTML:

var r = -1;

var mx = 499; // maximum
var a = new Array();

function AddNumsToDict(){
    var m,n,i,j;
    i = 0;
    j = 0;
    while (i <= 499)
    {
       m = (500 * Math.random())   1;
       n = Math.floor(m);

       for (j=0;j<=499;j  )
       {
         if (a[j] == (n-1))
         {
       j = -1;
           break;
         }
       }
       if (j != -1)
       {
     //a.push(n-1);
         a[i] = (n-1);
         i  ;
     j=0;
       }

    }
    return;

}

function startup()
{
  writit('SENTENCES<br><br><br>Robert Grenier', 'test');
  SetCookie("pg", -1);
  AddNumsToDict();

}

function SetCookie(sName, sValue)
{
  document.cookie = sName   "="   escape(sValue)   ";"

}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i  )
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}
function doBack(){
//var oPrev = xbElem('prev');
//var oNxt = xbElem('nxt');

//if ((oNxt) && (oPrev))
//{
    var num = GetCookie("pg");
    if (num == mx){ //maximum
      SetCookie("pg",parseInt(num) - 1);
      num = GetCookie("pg");
    }
//    oNxt.disabled = false;

//    if (num <= 1){
//      oPrev.disabled = true;
//    }
    if (num >= 1){
      num--;
      writit(Caption[a[num]], 'test');
      SetCookie("pg",num);
    }
//}
}

function doNext(){
//var oPrev = xbElem('prev');
//var oNxt = xbElem('nxt');
//  if ((oNxt) && (oPrev))
//  {
  var num = GetCookie("pg");
//  if (num > -1){
//    oPrev.disabled = false;
//  }
//  else{
//    oPrev.disabled = true;
//  }
//  if (num >= parseInt(mx)-1){  //maximum - 1
//    oNxt.disabled = true;
//  }
//  else {
//    oNxt.disabled = false;
//  }
  if (num <= parseInt(mx)-2){
    num  ;
    writit(Caption[a[num]],'test');
    SetCookie("pg",num);
  }
//  }
}
function writit(text,id)
{
        if (document.getElementById)
        {
                x = document.getElementById(id);
                x.innerHTML = '';
                x.innerHTML = text;
        }
        else if (document.all)
        {
                x = document.all[id];
                x.innerHTML = text;
        }
        else if (document.layers)
        {
                x = document.layers[id];
        l = (480-(getNumLines(text)*8))/2;
        w = (764-(getWidestLine(text)*8))/2;
                text2 = '<td id=rel align="center" CLASS="testclass" style="font:12px courier,courier new;padding-left:'   w.toString()   'px'   ';padding-top:'   l.toString()   'px'   '">'   text   '</td>';
                x.document.open();
                x.document.write(text2);
                x.document.close();
        }
}
function getNumLines(mystr)
{
  var a = mystr.split("<br>")
  return(a.length);
}
function getWidestLine(mystr)
{
  if (mystr.indexOf("&nbsp;") != -1)
  {
    re = /&nbsp;*/g;
    mystr = mystr.replace(re,"Z");
    //alert(mystr);
  }
  if (mystr.indexOf("<u>") != -1)
  {
    re = /<u>*/g;
        mystr = mystr.replace(re, "");
        re = /<\/u>*/g;
    mystr = mystr.replace(re, "");
  }

  if (mystr.indexOf("<br>") != -1)
  {
    var ss, t;
    var lngest;
    ss = mystr.split("<br>");
    lngest = ss[0].length;
    for (t=0; t < ss.length; t  )
    {
      if (ss[t].length > lngest)
      {
        lngest = ss[t].length;
      }
    }
  }
  else {
    lngest = mystr.length;
  }
  return(lngest);
}
// -->

</script>
<body bgcolor="gainsboro" onl oad="startup();">


<table bgcolor="white" border height="480px" width="764px" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<table nowrap>
<tr>
<td><img width="700px" height="1px" src="./resources/images/w.gif"></td>
<td>
<div  id="test"></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<center>
<form>
<p>

<input type="button" onclick="doBack(); return false" value="Back">
<input type="button" onclick="doNext(); return false" value="Next">

JS:

var _____WB$wombat$assign$function_____ = function(name) {return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name)) || self[name]; };
if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; return this; } }
{
  let window = _____WB$wombat$assign$function_____("window");
  let self = _____WB$wombat$assign$function_____("self");
  let document = _____WB$wombat$assign$function_____("document");
  let location = _____WB$wombat$assign$function_____("location");
  let top = _____WB$wombat$assign$function_____("top");
  let parent = _____WB$wombat$assign$function_____("parent");
  let frames = _____WB$wombat$assign$function_____("frames");
  let opener = _____WB$wombat$assign$function_____("opener");

function CaptionArray(len) {
    this.length=len
}
Caption = new CaptionArray(499);
Caption[0] = "leaf and the ants as latterly"
Caption[1] = "thought<br>living in<br>Davis would<br>be ok"
Caption[2] = "sure arm today"
Caption[3] = "AMY<br><br>no we<br>both do<br>different<br>songs together"
Caption[4] = "much of anything she doesn't like that at all"
Caption[5] = "SUNG AS LAKE<br><br><br>that never wanted back to come"
Caption[6] = "five sound shut doors"
Caption[7] = "oh<br>my nose is<br>so<br>red<br>Obediah<br>dear"
Caption[8] = "these<br>cubes<br>have been<br>on the floor"
Caption[9] = "sweating importunate"
Caption[10] = "all over noises phone rings"
Caption[11] = "I think this is the water supply for Lake Johnsbury"
Caption[12] = "Paw so greasy"
Caption[13] = "BLACK & WHITE RAIN<br><br><br>clear water grey drops<br><br><br>on windshields in a line<br><br><br>of cars progressing slowly<br><br><br>with windshield wipers wiping"
Caption[14] = "EMILY<br><br>Roger,<br><br>are you<br><br>thinking of me"
Caption[15] = "STICKS<br><br><br>rhythm is inside the sound like another"
Caption[16] = "I think their dog always barks when coming back from the woods"
Caption[17] = "weren't there<br><br>conversations"
Caption[18] = "LOOKING AT FIRE<br><br><u>ashes</u> to ashes<br><br>looking at the fire<br><br>at has been added"
Caption[19] = "a the bank"


}

CodePudding user response:

It looks like you're trying to implement a shuffle function for a collection of sentences in JavaScript. In order to achieve this, you can create a function that shuffles the sentences in the Caption array and then use that function to shuffle the sentences each time the user clicks on the "next" button.

Here is an example of how you could implement this:

var Caption = [  "Sentence 1",  "Sentence 2",  "Sentence 3",  ...];

// This function shuffles the elements in the given array
function shuffle(array) {
  // loop through the array and swap each element with a random other element in the array
  for (var i = 0; i < array.length; i  ) {
    var j = Math.floor(Math.random() * array.length);
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

// This function is called when the user clicks on the "next" button
function doNext() {
  // shuffle the sentences in the Caption array
  shuffle(Caption);

  // display the next sentence from the Caption array
  var num = GetCookie("pg");
  if (num <= parseInt(mx) - 2) {
    num  ;
    writit(Caption[num], "test");
    SetCookie("pg", num);
  }
}

This should allow the user to click on the "next" button to shuffle the sentences and display a new sentence each time. I hope this helps! Let me know if you have any other questions.

  • Related