简体   繁体   English

JavaScript 随机字(来自列表)生成器

[英]JavaScript random word (from a list) generator

I am new in JavaScript and I am very confused with the random command in JavaScript because in Python it's so different.我是 JavaScript 的新手,我对 JavaScript 中的随机命令感到非常困惑,因为在 Python 中它是如此不同。 I need help, because I want to made a Website on which you have 3 Buttons, when you press the first, only one random word from the first list is getting written in the text class above the Buttons and when you press the 2 Button only one Word from the 2. Array appears.我需要帮助,因为我想创建一个您有 3 个按钮的网站,当您按下第一个按钮时,只有第一个列表中的一个随机单词会写入按钮上方的文本 class 并且当您仅按下 2 个按钮时2. 数组中的一个单词出现。 But my JavaScript code doesn't work like it should.但是我的 JavaScript 代码不能正常工作。 Here is the Code.这是代码。

var Orte=('a','b','c')
var zufall=random(Orte) 
button1.onclick= document.getElementByclass('text')[0].innerHTML = 'zufall'

The DOM API you are looking for is: getElementsByClassName您正在寻找的 DOM API 是: getElementsByClassName

onclick needs to be a function that will be called on click event. onclick 需要是一个 function 将在点击事件上调用。 What you are doing is calling innerHTML='zufall' immediately, returning the string 'zufall' , and assigning onclick='zufall' , which is not what you want.你正在做的是立即调用innerHTML='zufall' ,返回字符串'zufall' ,并分配onclick='zufall' ,这不是你想要的。

JavaScript array literals are denoted with [] not (). JavaScript 数组字面量用 [] 而不是 () 表示。

I'm assuming the string 'zufall' is not what you want, but the variable zufall, which I've adjusted by removing the single quotes.我假设字符串 'zufall' 不是您想要的,而是变量 zufall,我通过删除单引号对其进行了调整。

Math.random() (not random() ) returns a random number from 0 to 1. Multiply that by the length and then floor to integer (You could use Math.trunc() also). Math.random() (不是random() )返回一个从 0 到 1 的随机数。将其乘以长度,然后乘以 integer (您也可以使用 Math.trunc() )。 And then in brackets to access that index in Orte.然后在括号中访问 Orte 中的该索引。

Again, variables are set when they are assigned.同样,变量是在分配时设置的。 Which is probably not what you want here.这可能不是你想要的。 The same thing happens in Python.同样的事情发生在 Python 中。 zufall would be assigned a value that doesn't change unless something is called to update it. zufall 将被分配一个不会改变的值,除非调用某些东西来更新它。 This is a common mistake, although as an experienced Python programmer you should not be making this mistake anymore.这是一个常见的错误,尽管作为一个有经验的 Python 程序员,你不应该再犯这个错误了。
I've adjusted it so it updates zufall on every click.我已经对其进行了调整,因此它在每次点击时都会更新 zufall。 As you should be able to see, a global variable is unnecessary.正如您应该能够看到的,全局变量是不必要的。 You can just generate the zufall only in the onclick handler.您只能在 onclick 处理程序中生成 zufall。

 var Orte = ['a', 'b', 'c'] var zufall = Orte[Math.random()*Orte.length | 0] button1.onclick = function(){ zufall = Orte[Math.random()*Orte.length | 0] document.getElementsByClassName('text')[0].innerHTML = zufall }
 <div class="text"></div> <button id="button1">click</button>

The best thing to do is to study a little but more about variables, arrays, Math.random() function.最好的办法是多研究一些变量,arrays,Math.random() function。 My contribution:我的贡献:

  • Variables: Best practice is to name them using camelCase format (More on best practices here: https://www.robinwieruch.de/javascript-naming-conventions )变量:最佳实践是使用 camelCase 格式命名它们(更多关于最佳实践的信息: https://www.robinwieruch.de/javascript-naming-conventions

  • Arrays: You define arrays using "[]". Arrays:您使用“[]”定义 arrays。 In your example, it should be:在您的示例中,它应该是:

    var orte = ['a','b','c']; var orte = ['a','b','c'];

    Also, it will be good to read about how you can access this values because you will be using this knowledge in your Math.random function.此外,最好了解如何访问这些值,因为您将在 Math.random function 中使用这些知识。

  • Random function: To understand correctly how to use the Math.random() function, please check this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random . Random function: To understand correctly how to use the Math.random() function, please check this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random .

  • When you use '' or "", anything in between is treated as a string.当您使用 '' 或 "" 时,两者之间的任何内容都被视为字符串。 So, in your code: "...innerHTML = 'zufall'", you are hardcoding the string "zuffal", not the variable.因此,在您的代码中:“...innerHTML = 'zufall'”,您正在硬编码字符串“zuffal”,而不是变量。

I tried to create a working example so you can compare:我试图创建一个工作示例,以便您进行比较:

https://codesandbox.io/s/naughty-cache-v7hxt https://codesandbox.io/s/naughty-cache-v7hxt

You can do it with plain JavaScript like this:您可以使用普通的 JavaScript 来做到这一点,如下所示:

 var arr1 = ["a", "b", "c"], arr2 = ["d", "e", "f"], arr3 = ["g", "h", "i"]; function showRandomFrom(chosenArray){ document.getElementsByClassName("text")[0].innerHTML = chosenArray[Math.floor(Math.random() * chosenArray.length)]; }
 <div class="text"></div> <button onclick="showRandomFrom(arr1);">From arr1</button> <button onclick="showRandomFrom(arr2);">From arr2</button> <button onclick="showRandomFrom(arr3);">From arr3</button>

Or like this with rando.js if you don't like randomness math:如果你不喜欢随机性数学,或者像这样使用rando.js

 var arr1 = ["a", "b", "c"], arr2 = ["d", "e", "f"], arr3 = ["g", "h", "i"]; var showRandomFrom = (chosenArray) => document.getElementsByClassName("text")[0].innerHTML = rando(chosenArray).value;
 <script src="https://randojs.com/1.0.0.js"></script> <div class="text"></div> <button onclick="showRandomFrom(arr1);">From arr1</button> <button onclick="showRandomFrom(arr2);">From arr2</button> <button onclick="showRandomFrom(arr3);">From arr3</button>

Also, JavaScript is a different language than Java.此外,JavaScript 是与 Java 不同的语言。 The code I've given you is JavaScript.我给你的代码是 JavaScript。

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

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