Hello. Trying to build an anagram game in Javascript to demo to students. I made the game last year in Python (much easier in Python), but it seems like this works well with the algorithm for using lists, functions and parameters.
The idea is that a user can choose the length of the word from a drop down menu, filter the list for only words of that length, return a random item from that filter list and then shuffle it and display it as an anagram.
I’ve gotten the filter and the random aspect to work, but can’t figure out how to shuffle.
In Python, I turned the string into an array and then shuffled the order of the items. I got the split string to an array to work, but not the shuffle.
I’ve consulted StackOverflow [(How to randomize (shuffle) a JavaScript array? - Stack Overflow] but I’m not sure if I’m returning or printing to console the right variable or if there is something more deeply wrong with the shuffleArray function.
Once I get those to work, I can plug them back in to my Anagram game (anyone else inspired by Wordle?)
Here is the code: App Lab - Code.org
Thanks for your help! I don’t see any of this in code.org documentation and I’m really new at Javascript (and coding in general).
Let’s look at two lines of code:
splitString(randomWord);
console.log(splitString);
The first line calls the function splitString
and throws the result into the garbage. You should assign it to a variable instead.
The second line prints the function `splitString’ to the console. As you can see from the output that isn’t very useful. Instead, print the variable you created above.
The next two lines of code have the same problem.
Thanks, as always, for your help. I had the split string working. It’s the shuffle that was the problem. I have it working now without a parameter and by just plugging in wordArray variable I had created from the splitString function.
Maybe I’m trying to use parameters where they are just not needed.
Using parameters and returned values makes a function easier to test. You can try it out in the REPL (read eval print loop) AKA Debug Console easily.
Functions that use parameters are also easier to turn into a library as we shall see in Unit 7.
Learning to code functions that way is a style that is good to conquer.
I suspected as much. Placeholders are more flexible than specific values. I’ve gotten both the split and shuffle to work now with parameters. Thanks for your help! Now on to finish the game.