Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 12-30-2006, 06:46 PM Javascript Puzzle
KRYPTOR's Avatar
Skilled Talker

Posts: 62
Name: Ray
Location: Illinois
Trades: 0

I have come up with a javascript that shuffles numbers 1-26, $1.00-$26.00 respectively,
with Case #'s as seen below...
I want to substitute 1-26 with the following array so that it will shuffle letters instead.

myArray = new Array (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y ,z);

What about the following 2 lines do I need them?

var randomNumber=Math.floor(Math.random()*26);
document.write(myArray[randomNumber]);

<SCRIPT Language = JavaScript>

function getNumbers() {
temp = 0
newnumber = 0
document.frmOne.taAll.value = ""
TA = document.frmOne.taAll
prize = new Array(26)

for (i = 1; i < 27; i++) {
prize[i] = i
}
for (i = 1; i < 27; i++) {
newnumber = (Math.random() * 26) + 1
newnumber = parseInt(newnumber, 10)
temp = prize[i]
prize[i] = prize[newnumber]
prize[newnumber] = temp
}
for (i = 1; i < 27; i++) {
TA.value = TA.value + "Case#[" + i + "] = " + prize[i] + "\n"
}

}

Thanks!
The KRYPTOR
__________________
"What is SEEN is TEMPORAL, What is UNSEEN is ETERNAL".


Please login or register to view this content. Registration is FREE
KRYPTOR is offline
Reply With Quote
View Public Profile Visit KRYPTOR's homepage!
 
 
Register now for full access!
Old 01-01-2007, 11:12 AM Re: Javascript Puzzle
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Hi, good work.
I'd recommend breaking things up a bit.

I'm posting a function to copy an array and one to shuffle an array. I'm then wrapping it in a generic object (based on you) to limit the namespace.
Code:
var KRYPTORutil = {
    copyArray:function(ary){
        var copy = new Array(ary.length);
        for(var i=0; i<ary.length; i++){
            copy[i] = ary[i];
        }
        return copy;
    },
    shuffleArray:function(ary){
        var aryOut = this.copyArray(ary);
        var size = aryOut.length;
        for( var i=0; i<size; i++){
            var rndIndex = Math.floor(Math.random() * size);
            var temp = aryOut[i];
            aryOut[i] = aryOut[rndIndex];
            aryOut[rndIndex] = temp;
        }
        return aryOut;
    }
};


// test
var letters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

var shuffledLetters = KRYPTORutil.shuffleArray(letters);
alert( shuffledLetters ); // see shuffled output

alert(letters); // insure original is unmodified
This gives you a generic utility function that can be used for other things as well. You can then pass the output of this to whatever needs a shuffled array.
HTH

Let me know if anything doesn't make sense
willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript Puzzle
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46452 seconds with 12 queries