Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with javascript array!! How to make the value no repeat?

    Hi, i really need everybody help. My project is using javascript & Html5, now i have a problem..... In my website, i have 9 buttons and there are 9 words display on the buttons. The words are get from xml file which are randomly display.
    Code:
    <question>
        <syllable>conti</syllable>
        <syllable>nental</syllable>
        <syllable>inter</syllable>
        <correct>intercontinental</correct>
      </question>
      <question>
        <syllable>pro</syllable>
        <syllable>reci</syllable>
        <syllable>cal</syllable>
        <correct>reciprocal</correct>
      </question>
    (part of the xml file)


    Javascript code:
    Code:
    if (window.XMLHttpRequest) {
                  xmlhttp = new XMLHttpRequest();
    
              }
              else {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              }
              xmlhttp.open("GET", "word.xml", false);
              xmlhttp.send();
              xmlDoc = xmlhttp.responseXML;
    
              var x = xmlDoc.getElementsByTagName("question");
    
    
              var i = Math.floor((Math.random() * 20) % x.length);
              var k = Math.floor((Math.random() * 20) % x.length);
              var s = Math.floor((Math.random() * 20) % x.length);
             
    
                  var array1 = new Array();
                   array1[0] = x[i].getElementsByTagName("syllable")[0].childNodes[0].nodeValue; 
                   array1[1] = x[i].getElementsByTagName("syllable")[1].childNodes[0].nodeValue;
                   array1[2] = x[i].getElementsByTagName("syllable")[2].childNodes[0].nodeValue;
    
                   var array2 = new Array();
                   array2[0] = x[k].getElementsByTagName("syllable")[0].childNodes[0].nodeValue;
                   array2[1] = x[k].getElementsByTagName("syllable")[1].childNodes[0].nodeValue;
                   array2[2] = x[k].getElementsByTagName("syllable")[2].childNodes[0].nodeValue; 
    
                   var array3 = new Array(); 
                   array3[0] = x[s].getElementsByTagName("syllable")[0].childNodes[0].nodeValue; 
                   array3[1] = x[s].getElementsByTagName("syllable")[1].childNodes[0].nodeValue;
                   array3[2] = x[s].getElementsByTagName("syllable")[2].childNodes[0].nodeValue;
    
                   var buttonArray = new Array();
                   buttonArray[0] = array1[0];
                   buttonArray[1] = array1[1];
                   buttonArray[2] = array1[2];
                   buttonArray[3] = array2[0];
                   buttonArray[4] = array2[1];
                   buttonArray[5] = array2[2];
                   buttonArray[6] = array3[0];
                   buttonArray[7] = array3[1];
                   buttonArray[8] = array3[2];
    
                   var random = new Array();
                           for (o = 0; o <= 9; o++) {
                               var ready = Math.floor(Math.random() * 9);
                               
                           }//FOR-LOOP
                         
                   document.write("<br/>");
                   document.write("<input type='button' id = 'one' value='" + buttonArray[random[0]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'two' value='" + buttonArray[random[1]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'three' value='" + buttonArray[random[2]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'four' value='" + buttonArray[random[3]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'five' value='" + buttonArray[random[4]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'six' value='" + buttonArray[random[5]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'seven' value='" + buttonArray[random[6]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'eight' value='" + buttonArray[random[7]] + "' onclick='show(this);'");
                   document.write("<br/>");
                   document.write("<input type='button' id = 'nine' value='" + buttonArray[random[8]] + "' onclick='show(this);'");
    now, i can get 3 set of the words, and display them randomly. BUT some of the words are repeated. How can I make them randomly display and NO repeat?????

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Shuffle your array of words.

    http://www.codingforums.com/post-javascript/264501-shuffling-arrays.html#post1239568

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 08-01-2013 at 02:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Try replacing these:

    Code:
    var i = Math.floor((Math.random() * 20) % x.length);
    var k = Math.floor((Math.random() * 20) % x.length);
    var s = Math.floor((Math.random() * 20) % x.length);
    with:

    Code:
    var len = x.length,
        i = Math.floor((Math.random() * 20) % len), 
        k, 
        s;
    
    do {
        k = Math.floor((Math.random() * 20) % len);
    }
    while (k == i);
    
    do {
        s = Math.floor((Math.random() * 20) % len);
    }
    while (s == i || s == k);

  • #4
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, glenngv. still the same... they just keep repeated..

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Use glenngv code but change "Math.floor((Math.random() * 20) % len)" to "Math.floor(Math.random() * len))".


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •