简体   繁体   English

我试图制作一个随机的单词生成器,为什么在我添加了一个将数字转换为文本的开关后,为什么它不会更新html?

[英]im trying to make a random word generator, why wont it update html after i added a switch to convert numbers to text?

   <ul id="myList">
  <li>Coffee</li>
  <li>Tea</li>
</ul>

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
for (i = 0; i =5 ; i++;) { 
var y = Math.floor((Math.random() * 46) + 1)
switch(y) {
    case 1: x="a"; break; case 2: x="i"; break;
  case 3: x="u"; break;  case 4: x="e"; break;
  case 5: x="o"; break; //vowels
  case 6: x="ka"; break; case 7: x="ki"; break;
  case 8: x="ku"; break;  case 9: x="ke"; break;
  case 10: x="ko"; break; //k's
  case 11: x="sa"; break; case 12: x="shi"; break; 
  case 13: x="su"; break;  case 14: x="se"; break;
  case 15: x="so"; break; //s's
  case 16: x="ta"; break;  case 17: x="chi"; break;
  case 18: x="tsu"; break;  case 19: x="te"; break;
  case 20: x="to"; break; //t's
  case 21:x="na"; break;   case 22:x="ni"; break;
  case 23:x="nu"; break;    case 24:x="ne"; break;
  case 25:x="no"; break;//n's
  case 26:x="ha"; break;   case 27:x="hi"; break;
  case 28:x="hu"; break;    case 29:x="he"; break;
  case 30:x="ho"; break;//h's
  case 31:x="ma"; break;   case 32:x="mi"; break;
  case 33:x="mu"; break;    case 34:x="me"; break;
  case 35:x="mo"; break;//m's
  case 36:x="ya"; break;   case 37:x="yu"; break;
  case 38:x="yo"; break;//y's
  case 39: x="ra"; break;  case 40: x="ri"; break; 
  case 41: x="ru"; break;   case 42: x="re"; break; 
  case 43: x="ro"; break;//r's
  case 44: x="wa"; break;  case 45: x="wo"; break;
  case 46: x="n"; break;//w's & n
     default: x="?"; break;
}

   var node = document.createElement("LI");
    var textnode = document.createTextNode(x);
    node.appendChild(textnode);
    document.getElementById("myList").appendChild(node);}

}
</script>

i put the JavaScript in script tags because the code i used as a reference point didn't work otherwise. 我将JavaScript放在脚本标签中是因为用作参考点的代码否则无法正常工作。
it worked well when i had everything but the for loop and the switch. 当我拥有for循环和开关以外的所有东西时,它工作得很好。 i was planing on bodging a chunk of code on js fiddle to generate random text to practice translating into a different script. 我打算在js小提琴上绑定一大堆代码,以生成随机文本以练习翻译成其他脚本。 (i generate 5 chunks at a time, write it down, move on to the next bit of code) i tried fiddling around with the switch and loop and fixed some semicolon errors but it still doesn't work. (我一次生成5个块,将其记下来,移至下一段代码)我尝试摆弄开关和循环并修复了一些分号错误,但仍然无法正常工作。 any ideas where i messed up? 有什么想法我搞砸了吗? edit: please don't edit my code on here, i have to put the script in the html otherwise it gets a error because it for some reason cant find the function. 编辑:请不要在这里编辑我的代码,我必须将脚本放在html中,否则会出现错误,因为由于某种原因找不到该函数。 if you have a idea on how to fix that error please let me know 如果您有关于如何解决该错误的想法,请告诉我

  1. You have a syntax error, your console is telling you that. 您有语法错误,您的控制台正在告诉您。 i++;) should be i++) i++;)应该是i++)

  2. You have an infinite loop. 您有一个无限循环。 for (i = 0; i = 5; i++) This is assigning 5 to i at every loop, so it will always be true. for (i = 0; i = 5; i++)这将在每个循环中将5分配给i,因此它将始终为真。

  3. Bonus : for a much simpler and cleaner code, you could use (I didn't do it all here because it would take too long) : 奖励:可以使用更简单,更简洁的代码(您在这里没有做所有的事情,因为这会花费太长时间):

    var letters = ["a", "i", "se", "shi"], // etc. y = Math.floor((Math.random() * letters.length), x = letters[y]; // Job done!

 var letters = ["a", "i", "se", "shi", "ke"] // Just add stuff here. function myFunction() { for (i = 0; i < 5; i++) { var y = Math.floor((Math.random() * letters.length)), x = letters[y], node = document.createElement("LI"), textnode = document.createTextNode(x); node.appendChild(textnode); document.getElementById("myList").appendChild(node); } } 
 <ul id="myList"> <li>Coffee</li> <li>Tea</li> </ul> <button onclick="myFunction()">Try it</button> 

如Kaddath所指出的那样,在增量声明之后有一个分号,并且将有一个无限循环: for (i = 0; i = 5; i++)应该是for (i = 0; i <= 5; i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM