Guest User

Untitled

a guest
Nov 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. var names = [
  2. "Alex",
  3. "Matthew",
  4. "Joe"
  5. ]
  6.  
  7. names.every(function(name){
  8. return name.length > 4
  9. })
  10.  
  11. names.some(function(name){
  12. return name.length > 4
  13. })
  14.  
  15.  
  16.  
  17.  
  18. // text input field
  19. function Field(value) {
  20. this.value = value
  21. }
  22.  
  23. Field.prototype.validate = function() {
  24. return this.value.length > 0
  25. }
  26.  
  27. var username = new Field("2cool")
  28. var password = new Field("8sdfb36_@eef")
  29.  
  30. // what if there will be much more fields. Like 20 fields
  31. // here the every or some function would be helpful
  32. username.validate() && password.validate()
  33.  
  34. var fields = [username, password]
  35.  
  36. var formsAreValid = fields.every(function(field) {
  37. return field.validate()
  38. })
  39.  
  40. if ( formsAreValid ) {
  41. // user can subscribe
  42. } else {
  43. // show error message
  44. }
  45.  
  46.  
  47.  
  48. // Online Course Tests
  49.  
  50. // every
  51. var users = [
  52. { id: 21, hasSubmitted: true },
  53. { id: 62, hasSubmitted: false },
  54. { id: 4, hasSubmitted: true }
  55. ];
  56.  
  57. var hasSubmitted = users.every(function(user) {
  58. return user.hasSubmitted === true
  59. })
  60.  
  61. // some
  62. var requests = [
  63. { url: '/photos', status: 'complete' },
  64. { url: '/albums', status: 'pending' },
  65. { url: '/users', status: 'failed' }
  66. ];
  67.  
  68. var inProgress = requests.some(function(request){
  69. return request.status === 'pending'
  70. })
Add Comment
Please, Sign In to add comment