This post summarizes a neat solution to a problem I ran into recently. Hope you find this useful!
Problem
&& is a well-known...
For further actions, you may consider blocking this person and/or reporting abuse
I'd avoid using
Array.prototype.every
in this way.It too will exit as soon as an entry returns false.
MDN - array.prototype.every
It's not because of
.every
that all of the validations run.It's because the array is created and all of its items are evaluated, before
.every
executesYou would get the same result with
Hope this helps
I thought the same! Good catch. All array methods that rely on the iteratee to return a boolean will stop execution as soon as possible.
I am still relatively new to Javascript so I appreciate this (I guess Stack Overflow doesn't always have the best solution haha). I will update my code. Thank you both!
Actually, reading at your code. It seems the thing you care about is to show the different error messages.
I guess, it's for showing inside a form.
So the condition for me before saving would be to check if errorMessage is empty or not.
However, like this way of using "every".
Thanks for reading and for the comment!
Will your solution work if the person only has a first name like Cher or Madonna?
In that case, you could customize your
lastNameIsValid
to something like this, so it allows for blank last names :)Results:
Good Tipps 👌🏻
Thank you!
Thanks for sharing.
Thanks for reading :)