Last active
December 13, 2022 15:53
-
-
Save LGitHub-sprout/8d9a2e74a87e028729668c7c2af1762e to your computer and use it in GitHub Desktop.
Examples from some() method MDN page - Nested Returns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MDN Example - Check Inventory (nested returns) | |
// See below link for converting any value to boolean | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | |
const fruit = ['kiwi', 'apple', 'banana', 'avocado']; | |
// Check inventory for 'pineapple' and then 'apple' | |
/* | |
Need to pass 'arr' and 'item' param. | |
If 'fruit' arg STRICTLY equals 'item' param, | |
some should return 'true'. | |
DON'T FORGET TO R-E-T-U-R-N ding-dong! | |
*/ | |
const checkAvailability = (arr, item) => { | |
// some() iterates over array - don't need to loop. | |
// return arr.some((el) => item === el); // works | |
// nested returns | |
// return arr.some((el, index, arr) => { | |
// return item === el; | |
// }); | |
// nested return | |
// result = arr.some((el, index, arr) => { | |
// return item === el; | |
// }); | |
// return result; | |
return arr.some((el, index, arr) => { | |
return item === el; | |
}); | |
}; | |
// needs to match exactly | |
console.log(checkAvailability(fruit, 'apple')); // s/b true/false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment