Closed smsgoldberg closed 1 year ago
Great job so far on this Sarah! But the shuffle is a little bit off. Here is the fisher-yates shuffle from this source
let Array = [25,1,5,10,40,100];
let len = Array.length;
let x;
function fy() {
for (x = len -1; x > 0; x--) {
let y = Math.floor(Math.random() * x)
let temp = Array[x]
Array[x] = Array[y]
Array[y] = temp
}
};
Thank you so much! This way worked much better.
What's the problem you're trying to solve?
After much wailing and gnashing of teeth, I finally managed to get a basic Fisher Yates shuffle to work on my array. Well, sort of -- the array seems bent on returning an additional instance of one object after the shuffle.
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
I'm not seeing an error message.
What is your best guess as to the source of the problem?
I'm not sure, because my instinct is still that this is some kind of indexing issue, but both attempts I made to fix it from that perspective failed.
What things have you already tried to solve the problem?
I tried writing (dogArray.length - 1) on the theory that it might be a weird indexing error, but this changed nothing. I also tried taking out i+1 in the Fisher Yates shuffle -- I struggled with the concept back when I was taking C++ and the JavaScript implemenation looks slightly different from what I remember.
Paste a link to your repository here
https://github.com/smsgoldberg/StayGolden