Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript - Array find() example (returns the value)
#1
The HTML part between the <body></body> tags looks like this:

Code:
<p>Click "Test" to return the value of the first element in the array that has a value above this number:</p>
<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Test</button>
<p id="demo"></p>


The JavaScript part looks like this:

Code:
const ages = [4, 12, 16, 20];

function checkAge(age)
{
  return age > document.getElementById("ageToCheck").value;
}

function myFunction()
{
  document.getElementById("demo").innerHTML = ages.find(checkAge);
}
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  JavaScript Array findIndex() examples (returns the index) Qomplainerz 0 474 04-04-2023, 08:10 AM
Last Post: Qomplainerz
  JavaScript exercises and solutions - Iterating through an array with keys and values Qomplainerz 0 410 04-04-2023, 07:52 AM
Last Post: Qomplainerz
  JavaScript - Get the length of an array Qomplainerz 0 408 04-04-2023, 07:45 AM
Last Post: Qomplainerz
  JavaScript array constructor Qomplainerz 0 411 04-04-2023, 07:43 AM
Last Post: Qomplainerz
  JavaScript Array copyWithin() example Qomplainerz 0 416 04-04-2023, 07:43 AM
Last Post: Qomplainerz
  JavaScript Array entries() example Qomplainerz 0 408 04-04-2023, 07:42 AM
Last Post: Qomplainerz
  JavaScript Array every() example Qomplainerz 0 403 04-04-2023, 07:42 AM
Last Post: Qomplainerz
  JavaScript Array fill() method examples Qomplainerz 0 419 04-04-2023, 07:41 AM
Last Post: Qomplainerz
  JavaScript - The find() method Qomplainerz 0 391 04-04-2023, 07:40 AM
Last Post: Qomplainerz
  Clone an array in JavaScript Qomplainerz 0 378 04-04-2023, 07:38 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)