<html>
<body>
<h1>JavaScript 数组</h1>
<p>单击按钮可返回数组中第一个值高于此数字的元素的索引:</p>
<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
const ages = [4, 12, 16, 20];
function checkAge(age) {
return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.findIndex(checkAge);
}
</script>
<p>Internet Explorer 不支持 findIndex() 方法。</p>
</body>
</html>