Algorithm Update


Challenge: How do I adapt Dan Shiffman’s distance algorithm so that the starting point on the path is always “the hive”?

For the sake of both clarity and accuracy, I decided that all students should begin their paths at the same point–the point where hive is located. This meant that I had to adjust the distance algorithm, because Dan Shiffman had designed it so that any point could be used for the starting position. After a bit of fumbling about in the JavaScript file, I remembered how the algorithm works! This is the sequence in which the path order gets tested (assuming a path array with a length of eight):

[0,1,2,3,7,6,5,4] etc.

What this means is that all the possibilities for any “starting position” (the point at index 0) are tested before the starting position is swapped. All I had to do was stop looping as soon as the first element in my array was no longer was equal to zero:

if (order[0] != 0) {

Once I figured that out, I was able to log the shortest and longest paths, seen below. I want the students to not only have access to the “optimal” path, but also be able to compare it to the “least optimal” path.


Leave a Reply

Your email address will not be published. Required fields are marked *