简体   繁体   English

如何创建 function 将随机字符串(来自数组)分配给某个变量?

[英]How do I create a function that shall assign a random string (coming from an array) to a certain variable?

No result is being produced by the codes below.以下代码不会产生任何结果。 I'm trying to assign a random item from an array to the constant called palindromeGenerator, and this word will be evaluated as a palindrome word or not (for which I'm yet to figure out the codes).我正在尝试将数组中的随机项分配给名为 palindromeGenerator 的常量,并且这个词将被评估为回文词或不被评估(我还没有弄清楚代码)。

 const palindromeGenerator = (random) => { var palindromeArray = [ "racecar", "noon", "mom", "tenet", "sagas", "hill", "programs", "computer", "internet" ]; var random = Math.floor(Math.random() * 10); if (random == 0) { palindromeArray[0]; }; else if (random == 1) { palindromeArray[1]; }; else if (random == 2) { palindromeArray[2]; }; else if (random == 3) { palindromeArray[3]; }; else if (random == 4) { palindromeArray[4]; }; else if (random == 5) { palindromeArray[5]; }; else if (random == 6) { palindromeArray[6]; }; else if (random == 7) { palindromeArray[7]; }; else if (random == 8) { palindromeArray[8]; }; else { palindromeArray[9]; }; } palindromeGenerator;

You do not return anything so nothing happens with the function call.您不返回任何内容,因此 function 调用不会发生任何事情。 You are also not calling the function.您也没有调用 function。 You have abunch of if statements to get the index when all you need to do is use the number generated.当您需要做的就是使用生成的数字时,您有大量的 if 语句来获取索引。

 var palindromeArray = ["racecar", "noon", "mom", "tenet", "sagas", "hill", "programs", "computer", "internet"]; const palindromeGenerator = () => { const random = Math.floor(Math.random() * palindromeArray.length); return palindromeArray[random]; } var pal = palindromeGenerator(); console.log(pal)

暂无
暂无

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

相关问题 如何将异步 function 的返回值分配给变量 - how do I assign a returned value from an async function to a variable 如何为全局变量分配功能? - How do I assign a function to a global variable? 如何在变量声明中使用匿名函数将字符串分配给变量? - How do I assign a string to a variable using an anonymous function in the variable declaration? 如果字符串很奇怪,我该如何将字符串粘贴并分配给javascript变量? - how do i paste and assign string to javascript variable, if string is weird? 如何使用 ejs 循环来自后端的变量 - How do I loop a variable coming from the backend using ejs 如何从我的 api 的响应中正确地将 products 变量分配给数组 - how do i properly assign the products variable to the array from the response from my api 如何从Javascript / jQuery中的字符串正确创建变量? - How do i properly create a variable from a string in Javascript/jQuery? 如何动态创建数组元素并将值(来自具有相同名称的变量)分配给元素 - How can I create array elements dynamically and assign values (from variable with the same names) to the elements 如何从数组中选择随机数据并确保从该数组中选择最少的特定数据而不影响概率? - How do I pick random data from an array and make sure that a minimum of a certain data are selected from that array without effecting probability? 我如何在localStorage中分配localForage中的变量? - How do I assign a variable from localForage as I do in localStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM