Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
If we break down the code, it is easier to see what is going on.
I'm gonna pull the function out of the forEach and create a new gte function.
functiongte(a,b){if(a>=b){returntrue;};returnfalse;};functiongreaterThan(numbr){array1.forEach(item=>gte(numbr,item));// ------------------------// /// Now it's easier to see that this doesn't do anything.returnfalse;};
Array.forEach's first argument is a function. I would recommend always breaking the function out to make the code more understandable.
When written this way, it is more clear what is going on and that you would not expect the forEach to return a value from the outer function greaterThan.
I also agree with kip, the better way would be some or every since it will break early on a false and not iterate the entire array.
I empower people to become software developers, especially those with kids/family responsibilities, full-time jobs, or who feel too old to start over. 🥰👩🏽💻
Location
Washington DC
Education
Duke University | The Firehose Project (coding bootcamp)
If we break down the code, it is easier to see what is going on.
I'm gonna pull the function out of the
forEach
and create a newgte
function.Array.forEach
's first argument is a function. I would recommend always breaking the function out to make the code more understandable.When written this way, it is more clear what is going on and that you would not expect the
forEach
to return a value from the outer functiongreaterThan
.I also agree with kip, the better way would be
some
orevery
since it will break early on afalse
and not iterate the entire array.Thank you so much for this!