14-PY-word-scramble.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //***************** Scene Object - Initialize When Scene Starts Event *************//
  2. // this function takes an array
  3. // shuffles it randomly and returns it
  4. $this.shuffle = function(arr){
  5. arr.sort(() => random(-10, 5));
  6. return arr;
  7. };
  8. $this.words = [
  9. "analog",
  10. "array",
  11. "binary",
  12. "browser",
  13. "computer",
  14. "condition",
  15. "conditional",
  16. "database",
  17. "download",
  18. "function",
  19. "group",
  20. "hardware",
  21. "interface",
  22. "javascript",
  23. "keywords",
  24. "layers",
  25. "megabyte",
  26. "network",
  27. "object",
  28. "program",
  29. "random",
  30. "routine",
  31. "scene",
  32. "script",
  33. "software",
  34. "sprite",
  35. "string",
  36. "syntax",
  37. "toggle",
  38. "variable",
  39. "visible",
  40. "website",
  41. "window",
  42. ];
  43. $this.getNewWord = function() {
  44. // clear out the guess word
  45. guess.text("");
  46. message.text("Unscramble the word: Click here for a new one.");
  47. // select a word at a random
  48. $this.selectedWord = $this.words[random($this.words.length - 1)];
  49. // create an empty array for the ordered letters
  50. var orderedLetters = [];
  51. // put each letter in the array
  52. for (var l = 0; l < $this.selectedWord.length; l++) {
  53. orderedLetters.push($this.selectedWord[l]);
  54. }
  55. // shuffle the letters in a new array
  56. var shuffledLetters = $this.shuffle(orderedLetters);
  57. // updated the shuffledLabel text
  58. shuffledLabel.text(shuffledLetters);
  59. };
  60. $this.checkGuess = function() {
  61. // only check if the guess is the correct length
  62. var originalWord = $this.selectedWord;
  63. var guessedWord = guess.text();
  64. if(originalWord.length === guessedWord.length) {
  65. message.text("Good job! Click here to play again!");
  66. // loop through letter in the guess
  67. for (var l = 0; l < originalWord.length; l++) {
  68. if (originalWord[l] != guessedWord[l]) {
  69. message.text("Wrong! Click here to try again!");
  70. }
  71. }
  72. }
  73. }
  74. if ($this.scene.state() == "PLAY") {
  75. $this.getNewWord();
  76. }
  77. //********************* resetButton Object - Mouse Click Event ********************//
  78. guess.text("");
  79. //******************** submitButton Object - Mouse Click Event ********************//
  80. $this.scene.checkGuess();
  81. //******************** message Object - Mouse Click Event ************************//
  82. $this.scene.getNewWord();