User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 373,471 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,932 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting

Trying to shuffle an array containing text

Join Date: Mar 2008
Posts: 1
Reputation: sweat17 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sweat17 sweat17 is offline Offline
Newbie Poster

Trying to shuffle an array containing text

  #1  
Mar 6th, 2008
i have a web page with an array of text messages which i want to 1) shuffle, and 2) scroll. first the shuffle part. right now i can scroll one random message per refresh of page. i want to shuffle the list and then change the scroll function to display all messages in random order. here is page now:

<html>
<head>
<script language="JavaScript" type="text/javascript">
var msg = new Array(5);
msg[0] = "First Message"
msg[1] = "Second Message"
msg[2] = "Third Message"
msg[3] = "Fourth Message"
msg[4] = "Fifth Message."
var count = 0;
var i = 0;
any = Math.floor(msg.length * Math.random());
while (i < 60) { i++; msg[any] = " " + msg[any]; }
function msgScroll() {
document.scroll.msg.value = msg[any].substring(count, msg[any].length);
count++;
if (msg[any].length == count) { count = 0; }
window.setTimeout("msgScroll()", 150);
}
window.onload=msgScroll;
</script>
</head>
<body>
<form name="scroll">
<p align="center">
<font color="#0000FF">
<input name="msg" size="125" style="border:0px solid; font-family:georgia; font-weight:700"></font></p>
</form>
</body>
</html>

i found following code to effect the shuffle but before i could customize to my page i get an error on 9th line: (trg = random(placearray.length); saying "object expected". can anyone point out my problem here?

arraythang = ["narf", "nood", "philbin", "twink", "snoogans"];
placearray = new Array();
newthang = new Array();
for (a=0; a<arraythang.length; a++) {
placearray.push(a);
}
for (a=0; a<arraythang.length; a++) {
do {
trg = random(placearray.length);
newplace = placearray[trg];
} while (arraythang[a] == arraythang[newplace] && a != arraythang.length-1);
placearray.splice(trg, 1);
if (arraythang[a] != arraythang[newplace]) {
newthang.push(arraythang[newplace]);
} else {
newthang.unshift(arraythang[newplace]);
}
}
trace (arraythang.toString());
trace (newthang.toString());
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 5:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC