调用返回的函数会出现”不是一个函数”的错误。

huangapple go评论96阅读模式
英文:

Calling a returned function gives "not a function"

问题

Sure, here is the translated content without the code:

我有这个假的 Wordle 游戏。
我认为问题出在获取 API 并返回验证函数方面。每当我按下提交按钮时,我会收到错误消息,指出 "getData(...).isCorrect" 不是一个函数。而当我在 index.html 中去掉括号尝试时,游戏就不会显示任何内容。是否有其他方法可以解决这个问题,或者这只是我的代码中的一个简单错误?

我希望这能帮助你。如果你需要进一步的帮助,请告诉我。

英文:

I have this fake wordle game
I think that the problem is with fetching the api and returning the functions for validation. I get an error that "getData(...).isCorrect is not a function" whenever I press submit and when I try without the parenthesis in index.html, the game won't display anything. Is there another way to do this, or is this just a simple bug in my code?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. async function getData() {
  2. var response = await fetch(&quot;https://all-wordle-words.kobyk1.repl.co/script.js&quot;);
  3. var data = await response.json();
  4. const wordsArray = data;
  5. const words = new Set(wordsArray);
  6. const chosen = wordsArray[Math.floor(Math.random() * wordsArray.length)];
  7. function check(word) {
  8. let result = Array(5).fill(&quot;gray&quot;);
  9. let chosenChars = [...chosen];
  10. for (let i = 0; i &lt; 5; i++) {
  11. if (word[i] === chosenChars[i]) {
  12. result[i] = &quot;green&quot;;
  13. chosenChars[i] = &quot;G&quot;;
  14. } else {
  15. for (let j = 0; j &lt; 5; j++) {
  16. if (word[i] === chosenChars[j]) {
  17. result[i] = &quot;yellow&quot;;
  18. chosenChars[j] = &quot;Y&quot;;
  19. }
  20. }
  21. }
  22. }
  23. return result;
  24. }
  25. function isCorrect() {
  26. let word = document.getElementById(&quot;guess&quot;).value.toLowerCase();
  27. if (words.has(word)) {
  28. result = check(word);
  29. let element = document.getElementById(&quot;guesses&quot;);
  30. element.innerHTML += colorResult(word, result);
  31. if (chosen === word) {
  32. alert(&quot;You found the word!&quot;);
  33. }
  34. } else {
  35. alert(&quot;Sorry, that word is not in our dictionary!&quot;);
  36. }
  37. }
  38. function colorResult(word, result) {
  39. word = word.toUpperCase();
  40. let columns = &quot;&quot;;
  41. for (let i = 0; i &lt; 5; i++) {
  42. columns += `&lt;td style=&quot;background-color: ${result[i]};&quot;&gt;${word[i]}&lt;/td&gt;`;
  43. }
  44. return &quot;&lt;tr&gt;&quot; + columns + &quot;&lt;/tr&gt;&quot;;
  45. }
  46. return {
  47. check,
  48. isCorrect,
  49. colorResult
  50. }
  51. }

<!-- language: lang-html -->

  1. &lt;h1&gt;KORDLE&lt;/h1&gt;
  2. &lt;table id=&quot;guesses&quot;&gt;
  3. &lt;tr&gt;&lt;/tr&gt;
  4. &lt;/table&gt;
  5. &lt;br&gt;
  6. &lt;input type=&quot;text&quot; id=&quot;guess&quot;&gt;
  7. &lt;button onclick=&quot;getData().isCorrect()&quot;&gt;Submit&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 2

根据异步函数,您的函数返回一个 promise 而不是一个对象。

所以尝试替换

  1. <button onclick="getData().isCorrect()">Submit</button>

  1. <button onclick="getData().then(obj => obj.isCorrect())">Submit</button>
英文:

According to async function, your function returns a promise not an object.

So just try to replace

  1. &lt;button onclick=&quot;getData().isCorrect()&quot;&gt;Submit&lt;/button&gt;

with

  1. &lt;button onclick=&quot;getData().then(obj =&gt; obj.isCorrect())&quot;&gt;Submit&lt;/button&gt;

答案2

得分: 0

你需要将异步函数与.then链式连接。

我会使用事件监听器,并更改函数的名称,因为它不仅仅获取数据。

  1. async function init() {
  2. var response = await fetch("https://all-wordle-words.kobyk1.repl.co/script.js");
  3. var data = await response.json();
  4. const wordsArray = data;
  5. const words = new Set(wordsArray);
  6. const chosen = wordsArray[Math.floor(Math.random() * wordsArray.length)];
  7. console.log(chosen);
  8. function check(word) {
  9. let result = Array(5).fill("gray");
  10. let chosenChars = [...chosen];
  11. for (let i = 0; i < 5; i++) {
  12. if (word[i] === chosenChars[i]) {
  13. result[i] = "green";
  14. chosenChars[i] = "G";
  15. } else {
  16. for (let j = 0; j < 5; j++) {
  17. if (word[i] === chosenChars[j]) {
  18. result[i] = "yellow";
  19. chosenChars[j] = "Y";
  20. }
  21. }
  22. }
  23. }
  24. return result;
  25. }
  26. function isCorrect() {
  27. let word = document.getElementById("guess").value.toLowerCase();
  28. if (words.has(word)) {
  29. result = check(word);
  30. let element = document.getElementById("guesses");
  31. element.innerHTML += colorResult(word, result);
  32. if (chosen === word) {
  33. alert("You found the word!");
  34. }
  35. } else {
  36. alert("Sorry, that word is not in our dictionary!");
  37. }
  38. }
  39. function colorResult(word, result) {
  40. word = word.toUpperCase();
  41. let columns = "";
  42. for (let i = 0; i < 5; i++) {
  43. columns += `<td style="background-color: ${result[i]};">${word[i]}</td>`;
  44. }
  45. return `<tr>` + columns + `</tr>`;
  46. }
  47. return {
  48. check,
  49. isCorrect,
  50. colorResult
  51. }
  52. }
  53. init().then((myInit) => {
  54. document.getElementById("sub").addEventListener("click", () => myInit.isCorrect());
  55. });
  1. <h1>KORDLE</h1>
  2. <table id="guesses">
  3. <tr></tr>
  4. </table>
  5. <br>
  6. <input type="text" id="guess">
  7. <button id="sub">Submit</button>
英文:

You need to chain the async function with a .then

I would use an eventListener and change the name of the function since it does way more than just getting data

  1. init().then((myInit) =&gt; {
  2. document.getElementById(&quot;sub&quot;).addEventListener(&quot;click&quot;, () =&gt; myInit.isCorrect());
  3. });

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

  1. async function init() {
  2. var response = await fetch(&quot;https://all-wordle-words.kobyk1.repl.co/script.js&quot;);
  3. var data = await response.json();
  4. const wordsArray = data;
  5. const words = new Set(wordsArray);
  6. const chosen = wordsArray[Math.floor(Math.random() * wordsArray.length)];
  7. console.log(chosen)
  8. function check(word) {
  9. let result = Array(5).fill(&quot;gray&quot;);
  10. let chosenChars = [...chosen];
  11. for (let i = 0; i &lt; 5; i++) {
  12. if (word[i] === chosenChars[i]) {
  13. result[i] = &quot;green&quot;;
  14. chosenChars[i] = &quot;G&quot;;
  15. } else {
  16. for (let j = 0; j &lt; 5; j++) {
  17. if (word[i] === chosenChars[j]) {
  18. result[i] = &quot;yellow&quot;;
  19. chosenChars[j] = &quot;Y&quot;;
  20. }
  21. }
  22. }
  23. }
  24. return result;
  25. }
  26. function isCorrect() {
  27. let word = document.getElementById(&quot;guess&quot;).value.toLowerCase();
  28. if (words.has(word)) {
  29. result = check(word);
  30. let element = document.getElementById(&quot;guesses&quot;);
  31. element.innerHTML += colorResult(word, result);
  32. if (chosen === word) {
  33. alert(&quot;You found the word!&quot;);
  34. }
  35. } else {
  36. alert(&quot;Sorry, that word is not in our dictionary!&quot;);
  37. }
  38. }
  39. function colorResult(word, result) {
  40. word = word.toUpperCase();
  41. let columns = &quot;&quot;;
  42. for (let i = 0; i &lt; 5; i++) {
  43. columns += `&lt;td style=&quot;background-color: ${result[i]};&quot;&gt;${word[i]}&lt;/td&gt;`;
  44. }
  45. return &quot;&lt;tr&gt;&quot; + columns + &quot;&lt;/tr&gt;&quot;;
  46. }
  47. return {
  48. check,
  49. isCorrect,
  50. colorResult
  51. }
  52. }
  53. init().then((myInit) =&gt; {
  54. document.getElementById(&quot;sub&quot;).addEventListener(&quot;click&quot;, () =&gt; myInit.isCorrect());
  55. });

<!-- language: lang-html -->

  1. &lt;h1&gt;KORDLE&lt;/h1&gt;
  2. &lt;table id=&quot;guesses&quot;&gt;
  3. &lt;tr&gt;&lt;/tr&gt;
  4. &lt;/table&gt;
  5. &lt;br&gt;
  6. &lt;input type=&quot;text&quot; id=&quot;guess&quot;&gt;
  7. &lt;button id=&quot;sub&quot;&gt;Submit&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 12:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定