Find Your Hat Challenge Project (JavaScript)

Wouldn’t be me if I didn’t pick on something trivial…

    let hatPosition = Math.floor(Math.random() * primeArrLength);
    if (hatPosition === startPosition) {
        do {
            hatPosition = Math.floor(Math.random() * primeArrLength);
        } while (hatPosition === startPosition);


  let hatPosition;
  do {
    hatPosition = Math.floor(Math.random() * primeArrLength);
  } while (hatPosition === startPosition);
1 Like

Thank you! I think I’ve put do while inside if several times, working now on correcting it all.

1 Like

Here is my solution:

this my solution[Find tour hat]
Esta fue mi respuesta al proyecto; si pueden verla y analizar algún error en la sintaxis, se los agradecería

This is my solution:

It includes user-configurable grid size, hole chance, and how often to add new holes (“hard mode”).

Field.findOpenSpot() could be optimized by maintaining an array of open spots on the grid and picking from that. The current implementation just picks a random location and loops until it finds one that isn’t occupied. This could potentially take a very long time if the field is mostly full and will infinite loop if the field is completely full, but I want to get on with the next lesson.

P-P-P-Private Prinny reporting! I have an emergency communication from M-M-M-Master Etna, dood!

"Ok, so what in all the netherworlds is going on with this challenge? My Prinny squad have been exceptionally attentive to me recently and it’s creeping me out! They have told me that they would much rather suffer their typical chores than be forced to attempt this challenge!!

They report that while they have been working through the Create Video Games with Phaser.js course that this challenge quest appeared out of nowhere while they were learning what classes were. It uses some sort of application that they have never encountered before, and even after having viewed the solution they say that what relation it even has to these classes is entirely unclear as they are barely used!

I don’t know what MY Prinnies are up to trying to learn this stuff, but this challenge has caused significant defects to them and I demand some sort of explanation as to what this challenge has to do with helping them learn, whatever it is they are learning!

Yours sincerely,

Beauty Queen Etna <3

PS. The only thing I approve of my Prinnies doing regarding this is checking out the answers to the problem."

S-s-s-so, there you have it! Please help us figure out just what we are supposed to be doing here, or Master Etna may adopt this challenge as a new method of punishment!

Hi Private Prinny, thanks for your report!

Please take this message back to your Master Etna:
"This ‘Find Your Hat’ Challenge Project is quite different from the usual fetch side quests that you may have ordered your Prinny squad members to do!

Instead of our usual practice Projects that tell you what code to write and where to write it, this project, a Challenge Project, does not! Instead it gives you the premise with broad/general instructions to guide you through the completion. As you can see, for a Prinny, this might be too big of an ask. It would require someone of at least your stature to take up and finish since it takes much longer to complete and more brain power to get the job done right!

This particular challenge project is also different from the JS/Phaser material because it’ll make use of a bash terminal and node to run your JavaScript code (rather than directly outputting in the terminal for you or have a browser interpret your code). But rest assured it’s still all JavaScript!

I also think that your Prinnies did well in looking over the solutions. These solutions can definitely help if you’re ever in a pinch/stuck, or if you want to improve your ‘code literacy’. If you’re up for the task and you want to practice using JavaScript in more general programming, try taking up this side quest for yourself! Otherwise, it’s not needed to continue on your Phaser main quest."

Hope that helps, safe travels Prinny!