1

I started learning JavaScript 4 days ago and I am trying to convert my python project to JavaScript for practice.

Here is my working python code:

def shuffle(l):
    for shuffles in range(numPerformed):
        heads = 0
        #tails = 0 
        toss = []
        for card in range(numCards):
            x = np.random.randint(2)
            toss.append(x)
            if x == 0:
                heads += 1
            #else:
                #tails += 1
        
        left = l[:heads] #left hand
        right = l[heads:] #right hand
        shuffled = []   #shuffled deck
        
        for card in range(numCards):
            if toss[card] == 0:
                shuffled.append(left.pop(0))
            if toss[card] == 1:
                shuffled.append(right.pop(0))
        l = shuffled
    return l

numCards = int(input("Enter number of cards in deck: "))
l = list(range(numCards))
numPerformed = int(input("Enter number of shuffles to perform on deck: "))
print(shuffle(l))

I am using the coin-toss method to do riffle shuffle. Here is my JS code, but something is causing it to give the wrong answer:

const list = (numCards) => {
    var newList = []
    var i = 0;
    while (i < numCards){
        newList.push(i);
        i++;
    }
    return newList
}

function shuffle(l, numPerformed){
    for (var shuffles = 0; shuffles < numPerformed; shuffles++){
        var heads = 0;
        var tails = 0;
        var toss = [];
        for (var card = 0; card < l.length; card++){
            var x = Math.floor(Math.random() * 2);
            toss.push(x);
            if (x == 0){
                heads++;
            }else{
                tails++;
            }
        }
        var left = []; //l[:heads]
        var right = []; //l[heads:]
        for (var i = 0; i < l.length; i++){
            i < heads ? left.push(i) : right.push(i);
        }

        var shuffled = [];
        for (var card = 0; card < numCards; card++){
            if (toss[card] == 0){
                shuffled.push(left.shift());
            }else if (toss[card] == 1){
                shuffled.push(right.shift());
            }
        }
        l = shuffled;
    }
    return l
}

var numCards = parseInt(prompt("Enter number of cards in deck: "))
var l = list(numCards)
var numPerformed = parseInt(prompt("Enter number of shuffles to perform on deck: "));
console.log(shuffle(l))

What am I doing wrong? I think this error is caused due to my inexperience with JavaScript syntax.

  • At first glance, seems like you aren't passing numPerformed into shuffle() Side note: You may find this useful: https://stackoverflow.com/questions/39924644/es6-generate-an-array-of-numbers – Matt Apr 27 '21 at 17:54

1 Answers1

0

I think, your code should be equivalent to this

const numCards = 52;
const l = new Array(numCards).fill().map((_, idx) => idx);
const numPerformed = 10;

const shuffle = (l, numPerformed) => {
    new Array(numPerformed)
    .fill()
    .map((_, idx) => idx)
    .forEach(_ => {
      const toss =
        new Array(numCards)
            .fill()
          .map(el => Math.random() > 0.5 ? 1 : 0);
      const heads = numCards - toss.reduce((acc, el) => acc + el, 0);
      const left = l.slice(0, heads);
      const right = l.slice(heads);
      const shuffled = [];
      
      new Array(numCards)
        .fill()
        .map((_, idx) => idx)
        .forEach(card => {
            if (toss[card] == 0) {
            shuffled.push(left.shift());
          }
          if (toss[card] == 1) {
            shuffled.push(right.shift());
          }
        })
        l = shuffled;
    });
    return l;
}

console.log(shuffle(l, numPerformed))
Virtuoz
  • 896
  • 1
  • 8
  • 14
  • Could you please explain what does the attached code mean? new Array(numPerformed) .fill() .map((_, idx) => idx) .forEach(_ => – Newly Coded Apr 27 '21 at 22:08
  • new Array(numPerformed) .fill() .map((, idx) => idx) is a JS equivalent of python range. It would be nice to extract it to the separate function. forEach iterates over each element of the collection and executes a function that passed as a forEach argument for each element. – Virtuoz Apr 28 '21 at 05:40