www.webdeveloper.com
Results 1 to 4 of 4

Thread: js array shuffle won't work

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    15

    Question js array shuffle won't work

    Hi, having a major problem with a shuffle function....when shuffle is called, the numbers come back with commas and the array runs out of the div. I can't figure out how to add a space between the numbers.
    What I would like is to have each index location in the array be put inside a separate div and locate all of those divs within one div (id-"numberbox"). So I would end up with 1 through n, each in it's own div and they would shuffle when the shuffle button is clicked. It kind of works a little bit sort of. Here's what I've got so far.....
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    
    <script type="text/javascript" src="jquery.js"></script>
    
    <script type="text/javascript">
    var nums = []
    function display_array() {
    	 n = document.getElementById('input').value ;
    		for (var i = 1; i <= n; i++) {
    	    document.getElementById('numberbox').innerHTML +=(nums.push(i)+" ");
    		
    }
    
    }
    function shuffle() {
    
    	    
    		document.getElementById('numberbox').innerHTML =" ";{
    	shuffle = function(o){ //v1.0
    	for(var j, x, i = o.length; i; j = parseInt(Math.random() * n), x = o[--i], o[i] = o[j], o[j] = x);
    	return o;
    };	
    newArray = shuffle(nums);
    document.getElementById('numberbox').innerHTML +=newArray;
    
    
    }
    }
    	
    
    
    
    
    function reset_counter() {
    	
    		document.getElementById('numberbox').innerHTML = '';
    		document.getElementById('input').value = '';
    		document.getElementById('input').focus();
    		nums.length = 0;
    }
    window.onload = function() {
      document.getElementById("input").focus();
      
    }
    </script>
    </head>
    <body style="height:100px;width:100%;background-color:#000000;color:#ffffff;font-weight:bold;">
    <div id="header" style="height:100px;width:100%;">
    
    
    <center>
    <h1>Numbermer Guess</h1>
    
    <input type="text"   size="2"   style="border: 6px solid #C0C0C0; text-align: center; font-size: 18PX; font-family: impact, times new roman; color: #B0C4DE; background-color: #190033; " id="input">
    <input type="button" class="buttons" style="color:  #B0C4DE; font-weight: bold; border: 6px solid #C0C0C0; background-color: #190033;" onclick="display_array()" size="2"  value="GO">
    <input type="button" style="color:  #B0C4DE; font-weight: bold; border: 6px solid #C0C0C0; background-color: #190033;" onclick="shuffle()" size="15"  value="SHUFFLE NUMBERS">
    <input type="reset" style="color: #B0C4DE; font-weight: bold; border: 6px solid #C0C0C0; background-color: #190033;" onclick="reset_counter()"  value="RESET">
    </center>
    
    </div>
    <div id="left" style="width:20%;height:100%;float:left;">
    
    </div>
    <div id="content" style="float:left;width:60%; height:100%;">
    
    <center>
    <div id="numberbox" style="border:6px solid blue;font-size:40px;width:80%; height:100%;color:blue;text-align:center;padding-top:50px;">
    
    </div>
    </center>
    </div>
    <div id="right" style="width:20%;height:100%;float:right;">
    <center>
    <p>INSTRUCTIONS</p>
    </center>
    </div>
    </body>
    </html>
    Anyone with any ideas would be appreciated. thanks.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    try this

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>shufl</title>
    <style type="text/css">
    body{background-color:#000000;color:#ffffff;font-weight:bold;text-align:center;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;}
    div{text-align:center;}
    div.result{display:inline-block;margin-left:10px;margin-right:10px;}
    #header{height:100px;width:100%;}
    #inp{border:6px solid #c0c0c0;text-align:center;font-size:18px;font-family:impact,'times new roman';color:#B0C4DE;background-color:#190033;}
    #left_div{display:inline-block;height:100%}
    #numberbox{display:inline-block;border:6px solid blue;font-size:40px;width:60%;color:blue;margin-top:50px;}
    #right_div{display:inline-block;height:100%}
    .butn{color:#b0c4de;font-weight:bold;border:6px solid #c0c0c0;background-color:#190033;cursor:pointer;}
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    Array.prototype.shuffle=function(){
    for(var i=this.length-1; i>0; i--){
    var num=Math.floor(Math.random()*(i+1)),d=this[num];
    this[num]=this[i];this[i]=d;
    }
    return this;
    }
    
    var nums=[];
    
    function display_array(){
    nums.length=0;
    var n=$('#inp').val(),mask=/[\d]{1,2}/ig,output='';
    if(!mask.test(n)){output='Wrong input format';}
    if(n<2){output='The array is too short';}
    else{nums.length=0;for(var i=1;i<=n;i++){nums.push(i);output+='<div class="result">'+i+'</div>';}}
    $('#numberbox').html(output);
    }
    
    function shufl(){
    if(nums.length>0){
    nums.shuffle();
    var output='';
    for(var i=0; i<nums.length; i++){output+='<div class="result">'+nums[i]+'</div>';}
    $('#numberbox').html(output);
    }
    else{$("#inp").focus();}
    }
    
    function reset_counter(){
    $('#numberbox').html('&nbsp;');
    $('#inp').val('');
    $('#inp').focus();
    nums.length = 0;
    }
    
    $(document).ready(function(){
    $('#but_go').click(function(){if($('#inp').val()!==''){display_array();}else{$("#inp").focus();}});
    $('#but_shuf').click(function(){shufl();});
    $('#but_res').click(function(){reset_counter();});
    $("#inp").focus();
    });
    </script>
    </head>
    <body>
    <div id="header">
    <h1>Numbermer Guess</h1>
    <input id="inp" type="text" size="2" maxlength="2">
    <input id="but_go" type="button" class="butn" size="2" value="GO">
    <input id="but_shuf" type="button" class="butn" size="15" value="SHUFFLE NUMBERS">
    <input id="but_res" type="button" class="butn" value="RESET">
    </div>
    <div id="content">
    <div id="left_div">left_div</div>
    <div id="numberbox">&nbsp;</div>
    <div id="right_div"><p>INSTRUCTIONS</p></div>
    </div>
    </body>
    </html>
    Last edited by Padonak; 05-05-2013 at 02:47 AM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Oct 2012
    Posts
    15
    Wow, works perfectly, I've spent forever trying everything and nothing worked.
    I surely do appreciate your help and thanks so much for your time.
    The help this site provides people like me is invaluable.
    Thanks again Padonak and have a good day, mine just got much brighter.

    Oh, moving the css to one spot is a nice touch and looks much nicer and less jumbled, thanks.
    Last edited by Baldy; 05-05-2013 at 09:14 AM.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    good to hear that it helped )) have fun, buddy )
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles