www.webdeveloper.com
Results 1 to 4 of 4

Thread: js array shuffle won't work

  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,716
    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.
    signature under construction

  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,716
    good to hear that it helped )) have fun, buddy )
    signature under construction

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17255 seconds
  • Memory Usage 2,882KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates