Code feedback

//test Array
const testArr = ['door1','door2','door3'];

//The function 
function arrayScrambler (arr) {
    let resultArray = [];
        do {
            let randomIndex = Math.floor((Math.random() * arr.length));
        }while(resultArray.length != arr.length);
    return resultArray;    

//test the function
alert(arrayScrambler(testArr).join(' '))

Greetings my friends.
I created this code for a codecademy exercise but I wonder if there is a better way of achieving the same results.
The function takes an array and returns a new array with the same content but in random positions.
Have a nice day :smiley:


Hello :slight_smile:

First of all, if arr contains duplicate values (for example arrayScrambler(['door1','door2','door3','door3'])) evaluation of your function will get stuck in an infinite loop. And it really does not matter how you want to handle duplicated values, infinite loop is never a good option.

This might be not an issue, depending on the usage, but you asked for feedback. Your function creates a temporary array to store the result and I don’t see any reason to use extra memory to store this array. You can simply “shuffle” the array in-place. You can take a look at the Fisher–Yates shuffle algorithm, it is super easy to implement and it does not use any extra memory (except auxiliary variables).


Sometimes data structures have their own inherent logic…

const randomChoreDoorGenerator = () => {
  return [
    [botDoorPath, beachDoorPath, spaceDoorPath],
    [spaceDoorPath, botDoorPath, beachDoorPath],
    [beachDoorPath, spaceDoorPath, botDoorPath]
  ][Math.floor(Math.random() * 3)];

In each of the arrays the names represent URLs.


These are great advices😊 Thank you.


I was also wondering…
Which other useful algorithms do you recommend to have in mind?


It’s kind of impossible to provide a list of useful algorithms. So forgive me, but I will not even try to do that :slight_smile:

Computer science is such a broad subject that it’s really hard to say that something is useful or not. If you are not interested in computer vision you will never have to implement a Sobel operator, which is one of fundamental tools in this domain, so is it useful or not? It depends on your interests.

Take a look at this discussion, you posted a question related to algorithms and you got two Codecademy moderators chimming in to share their thoughts. In the result you have one more algorithm in your arsenal. And this is how it works. Keep up this attitude and your arsenal of algorithmic tools will grow larger.

But there is something I can do, I can give you an advice. If you are interested in algorithmics (and it seems like you are!) there are many great resources that are worth of time investment. For me, personally, these two books were game changing:

  1. The Art of Computer Programming by Donald Knuth. I can’t stress enough how awesome these books are (there are several volumes and more to come in the future). Donald Knuth has extraordinary ability to infect readers with a love for algorithms. Truly a masterpiece.
  2. Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. In many universities around the world this book is a main textbook for algorithm courses. Every chapter describes different algorithm and descriptions of algorithms are written in a way to ensure understanding of the process. For me this was a one time read, but I have colleagues who keep this book on their desks in the workplace :slight_smile: Worth reading, for sure.


This is great stuff! Thank you so much for sharing with me.
I did in fact implement the algorithm you suggested in my own code base :smiley: so I can grab it when ever I need it.
I’ll definitely check out the books you mentioned.
Many thanks.


You’re very welcome :panda_face: