[英]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.