Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39482 > unrolled thread

Getting 5 random cards at a time.

Started byKristjan Robam <he12457@hotmail.com>
First post2021-02-16 04:14 -0800
Last post2021-02-16 11:51 -0800
Articles 7 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Getting 5 random cards at a time. Kristjan Robam <he12457@hotmail.com> - 2021-02-16 04:14 -0800
    Re: Getting 5 random cards at a time. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-02-16 06:19 -0800
      Re: Getting 5 random cards at a time. Kristjan Robam <he12457@hotmail.com> - 2021-02-16 07:16 -0800
        Re: Getting 5 random cards at a time. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-02-16 07:56 -0800
          Re: Getting 5 random cards at a time. Mabor Najtcirk <he12457@hotmail.com> - 2021-02-18 13:49 -0800
            Re: Getting 5 random cards at a time. Mabor Najtcirk <he12457@hotmail.com> - 2021-02-18 13:49 -0800
        Re: Getting 5 random cards at a time. "sc...@sauyet.com" <scott@sauyet.com> - 2021-02-16 11:51 -0800

#39482 — Getting 5 random cards at a time.

FromKristjan Robam <he12457@hotmail.com>
Date2021-02-16 04:14 -0800
SubjectGetting 5 random cards at a time.
Message-ID<bd0e7131-00fd-4f81-9ae1-1f90a142aa79n@googlegroups.com>
<html><body>

<script>

var y=new Array(2,3,4,5,6,7,8,9,10,'J','Q','K','A','Jo');
var y2=new Array('Hearts','Diamonds','Spade','Clubs');

function y1(a,a2) {
   for(var i=Math.round(a/a2)-1; i<=a; i++) {
     if((i*a2<=a)&&(a<=(i+1)*a2)) {
         return a-i*a2;
         break;
     }
   }
}
function Card() {
   var v01=y[y1(Math.round(Math.random()*7458733246324627),y.length)-1]
   var v02=y2[y1(Math.round(Math.random()*574354757257835),y2.length)-1];
   if(v01=='Jo') return v01;
    else return v01+" "+v02;
}

var f1=0;
var f2="";
function f() {
   if(f1<5) {
   f1++;
   setTimeout(function() { f2=f2+" "+Card(); f();}, Math.random()*100);
   } else {
     document.write(f2);
   }
}
f();

</script>
</body>
</html>


Kristjan Robam

[toc] | [next] | [standalone]


#39483

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2021-02-16 06:19 -0800
Message-ID<7b623c9a-7905-4c75-b4bb-5bc895c962bbn@googlegroups.com>
In reply to#39482
On Tuesday, February 16, 2021 at 6:14:37 AM UTC-6, Kristjan Robam wrote:
> <html><body> 
> 
> <script> 
> 
> var y=new Array(2,3,4,5,6,7,8,9,10,'J','Q','K','A','Jo'); 
> var y2=new Array('Hearts','Diamonds','Spade','Clubs'); 
> 
> function y1(a,a2) { 
> for(var i=Math.round(a/a2)-1; i<=a; i++) { 
> if((i*a2<=a)&&(a<=(i+1)*a2)) { 
> return a-i*a2; 
> break; 
> } 
> } 
> } 
> function Card() { 
> var v01=y[y1(Math.round(Math.random()*7458733246324627),y.length)-1] 
> var v02=y2[y1(Math.round(Math.random()*574354757257835),y2.length)-1]; 
> if(v01=='Jo') return v01; 
> else return v01+" "+v02; 
> } 
> 
> var f1=0; 
> var f2=""; 
> function f() { 
> if(f1<5) { 
> f1++; 
> setTimeout(function() { f2=f2+" "+Card(); f();}, Math.random()*100); 
> } else { 
> document.write(f2); 
> } 
> } 
> f(); 
> 
> </script> 
> </body> 
> </html> 

Do you have a question that goes with this code? I know I have some...

[toc] | [prev] | [next] | [standalone]


#39484

FromKristjan Robam <he12457@hotmail.com>
Date2021-02-16 07:16 -0800
Message-ID<f43da398-c8eb-49da-9f6c-ea57b76ffb18n@googlegroups.com>
In reply to#39483
There is no question, that i want to ask.
What questions do You have ?


A little fix to the code, for it to look more exciting:


<html><body>

<script>

var y=new Array(2,3,4,5,6,7,8,9,10,'J','Q','K','A','Jo');
var y2=new Array('Hearts','Diamonds','Spade','Clubs');

function y1(a,a2) {
for(var i=Math.round(a/a2)-1; i<=a; i++) {
if((i*a2<=a)&&(a<=(i+1)*a2)) {
return a-i*a2;
break;
}
}
}
function Card() {
var v01=y[y1(Math.round(Math.random()*7458733246324627),y.length)-1]
var v02=y2[y1(Math.round(Math.random()*574354757257835),y2.length)-1];
if(v01=='Jo') return v01;
else return v01+" "+v02;
}

var f1=0;
var f2="";
function f() {
if(f1<5) {
f1++;
setTimeout(function() { f2=f2+" "+Card(); f();}, Math.random()*1517);
} else {
document.write(f2);
}
}
f();

</script>
</body>
</html>




Kristjan Robam 

[toc] | [prev] | [next] | [standalone]


#39485

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2021-02-16 07:56 -0800
Message-ID<3693baa9-5089-4a66-b064-314c75a753f9n@googlegroups.com>
In reply to#39484
On Tuesday, February 16, 2021 at 9:16:19 AM UTC-6, Kristjan Robam wrote:
> There is no question, that i want to ask. 
> What questions do You have ? 

1. Why are you not using array literals?
2. Why do you have a break statement after a return statement
3. Why are you using the numbers '7458733246324627' and '574354757257835' in your array index?
4. Why are some of the entries numbers and others strings?
5. Why are you using document.write?
6. What is going on with that random setTimeout?
7. etc...

[toc] | [prev] | [next] | [standalone]


#39494

FromMabor Najtcirk <he12457@hotmail.com>
Date2021-02-18 13:49 -0800
Message-ID<ab8e6a62-04b1-497e-abc4-1861893a9051n@googlegroups.com>
In reply to#39485
I have no question.
Change to code, for it to look more exciting :

 <html><body>

 <script>
 var y=new Array(2,3,4,5,6,7,8,9,10,'J','Q','K','A','Jo');
 var y2=new Array('Hearts','Diamonds','Spade','Clubs');

 function y1(a,a2) {
 for(var i=Math.round(a/a2)-1; i<=a; i++) {
 if((i*a2<=a)&&(a<=(i+1)*a2)) {
 return a-i*a2;
 break;
 }
 }
}
 function Card() {
 var v01=y[y1(Math.round(Math.random()*7458733246324627),y.length)-1]
 var v02=y2[y1(Math.round(Math.random()*574354757257835),y2.length)-1];
 if(v01=='Jo') return v01;
 else return v01+" "+v02;
 }

 var f1=0;
 var f2="";
function f() {
 if(f1<5) {
 f1++;
 setTimeout(function() { f2=f2+" "+Card(); f();}, Math.random()*1426);
 } else {
 document.write(f2);
 }
 }
 f();

 </script>
 </body>
 </html> 


Mabor Najtcirk

[toc] | [prev] | [next] | [standalone]


#39495

FromMabor Najtcirk <he12457@hotmail.com>
Date2021-02-18 13:49 -0800
Message-ID<464232dc-84d8-40b3-859e-1f10c855d727n@googlegroups.com>
In reply to#39494
I have no question.
Change to code, for it to look more exciting :

<html><body>

<script>
var y=new Array(2,3,4,5,6,7,8,9,10,'J','Q','K','A','Jo');
var y2=new Array('Hearts','Diamonds','Spades','Clubs');

function y1(a,a2) {
for(var i=Math.round(a/a2)-1; i<=a; i++) {
if((i*a2<=a)&&(a<=(i+1)*a2)) {
return a-i*a2;
break;
}
}
}
function Card() {
var v01=y[y1(Math.round(Math.random()*7458733246324627),y.length)-1]
var v02=y2[y1(Math.round(Math.random()*574354757257835),y2.length)-1];
if(v01=='Jo') return v01;
else return v01+" "+v02;
}

var f1=0;
var f2="";
function f() {
if(f1<5) {
f1++;
setTimeout(function() { f2=f2+" "+Card(); f();}, Math.random()*1426);
} else {
document.write(f2);
}
}
f();

</script>
</body>
</html>


Mabor Najtcirk 

[toc] | [prev] | [next] | [standalone]


#39487

From"sc...@sauyet.com" <scott@sauyet.com>
Date2021-02-16 11:51 -0800
Message-ID<c279ea6e-c44d-4ae2-afa5-9ab18a9f8ed0n@googlegroups.com>
In reply to#39484
Kristjan Robam wrote:

> There is no question, that i want to ask. 
> What questions do You have ? 

All of those from Micheal Haufe, plus

  - Can you think of anything simpler?
  - Have you tried a shuffle?

If I were to try to create a deck, and draw a hand of five cards from it, I might do something like this:

```
// Note that this returns a hand AND mutates the deck by removing those cards

const draw = (n) => (d, i = Math .floor (Math .random () * d .length)) =>
  n == 0 || n > d .length
    ? []
    : [d [i], ...draw (n - 1) (d.splice(i, 1) && d)]

const pokerHand = draw (5)

const deck = ['clubs', 'diamonds', 'hearts', 'spades'] .flatMap (suit => 
  '2-3-4-5-6-7-8-9-10-J-Q-K-A' 
    .split ('-') 
    .map (rank => ({rank, suit}))
)

console .log (pokerHand (deck)) //=>
// [                    // for example
//   {rank: "3", suit: "spades"}, 
//   {rank: "K", suit: "diamonds"},
//   {rank: "A", suit: "diamonds"}, 
//   {rank: "10", suit: "spades"},
//   {rank: "10", suit: "clubs"}
// ]

console .log (deck .length) //=> 47
```

Does this make sense?

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web