From 99bd2c1dbb5d2bb4a4322cdb717d0a32aa6b31d4 Mon Sep 17 00:00:00 2001 From: Melvin Viana <35651825+Melvin-Viana@users.noreply.github.com> Date: Tue, 6 Nov 2018 19:46:44 -0800 Subject: [PATCH] Update "Finders-keepers" guide index.md (#34217) * Update index.md My proposed advanced solution * Update index.md --- .../finders-keepers/index.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/index.md index 02c6b45725..596b8ff092 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/index.md @@ -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.