简体   繁体   English

我如何将许多随机生成的整数转换为数组的字符串,以便执行 forEach() 方法来检查 age >= 18 [DESC]

[英]How could I convert many randomly generated integers into a string of an array in order to perform the forEach() method to check if age >= 18 [DESC]

I am trying to experiment an unusual method of converting randomly generated integers into strings and then converting the strings into an array list with the generated numbers by using the split() method, this is so that I may perform a function on each element inside the array using the forEach() method.我正在尝试一种不寻常的方法,将随机生成的整数转换为字符串,然后使用 split() 方法将字符串转换为包含生成数字的数组列表,这样我就可以对内部的每个元素执行 function使用 forEach() 方法的数组。

My goal is that the forEach() method checks to see if all the randomly generated integers inside the array are greater than or equal to 18, otherwise return 'lower than 18', age我的目标是 forEach() 方法检查数组内是否所有随机生成的整数都大于或等于 18,否则返回 'lower than 18', age

This is what I have come with so far and I'm stuck:到目前为止,这就是我所带来的,但我被困住了:

<p id="demo"></p>
<button onclick="myFunction()">Try</button>
var numbers1 = Math.floor(Math.random() * 25 + 1).toString()
var numArr = numbers1.split(",").map(createAnArray)

function createAnArray(item) {
  return parseInt(item)
}
console.log(numArr)

function myFunc(age) {
  if (age >= 18) {
   return "age suceeds", age >= 18;
  }
  else if (age != 18 || age === 17) {
  document.getElementById('demo').innerHTML = 'lower than 18', age;
  } else {
  return 'flop'
  }
}

function myFunction() {
  document.getElementById("demo").innerHTML = numArr.find(myFunc);
}

I think you probably want something like this我想你可能想要这样的东西

 function randomNumbers(maximum, minimum, arrlen) { return Array.from( // create an array { length: arrlen }, // give it an optional length () => (Math.floor(Math.random() * (maximum - minimum + 1)) + minimum) // generate random numbers in a range ); } console.log(randomNumbers(30, 15, 10));

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

相关问题 我需要遍历一个数组并找到 18 岁以下的人。我应该先将其转换为 String (toString) 吗? - I need to loop through an array and find the people that are under age 18 . Should i convert it to String (toString) first? 将字符串转换为整数数组 - convert string into array of integers 如何将字符串转换为32位整数数组? - how to convert string to array of 32 bit integers? 如何将字符串表示形式转换为数组整数? - How to convert a string representation to an array integers? 我有对象数组,我的目标是仅使用 forEach 按升序打印对象的年龄 - I have array of objects, My goal is to print age of objects in ascending order using forEach only 如何对定义为字符串的变量执行 forEach 循环 | 大批<string> ?</string> - How to perform a forEach loop on a variable defied as string | Array<string>? 如何在另一个区域再次显示随机生成的字符串(来自数组)? - How to display a randomly generated string (from array) again in another area? 我如何检查字符串是否包含数组的元素 - How could i check if a string contains an element of an array 使用随机生成的值检查数组中的重复项 - Check for duplicates in array with randomly generated values 我将如何附加一个随机生成的字符串来响应路由器 useParams()? - How would I attach a randomly generated string to react router useParams()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM