简体   繁体   中英

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.

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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