Update "Finders-keepers" guide index.md (#34217)
* Update index.md My proposed advanced solution * Update index.md
This commit is contained in:
committed by
Paul Gamble
parent
dfc68943f7
commit
99bd2c1dbb
@ -39,3 +39,19 @@ function findElement(arr, func) {
|
||||
* The `num` variable is being passed into the function, so we set it to each index in our array.
|
||||
* The pre-defined function already checks each number for us, so if it is "true", we return that num.
|
||||
* If none of the numbers in the array pass the function's test, we return undefined.
|
||||
|
||||
|
||||
## Advanced Solution
|
||||
|
||||
```javascript
|
||||
function findElement(arr, func) {
|
||||
return arr[(arr.map(func)).indexOf(true)];
|
||||
}
|
||||
```
|
||||
|
||||
## Code Explanation
|
||||
|
||||
1. Look through the array given in the 1st paramater "arr" using the .map() method
|
||||
2. Use the function in the 2nd parameter as the callback function in arr.map()
|
||||
3. Acquire the index of the first number that meets the condition in the function.
|
||||
4. Use that index to display the first available number that meets the condition.
|
||||
|
Reference in New Issue
Block a user