JSFiddle - React, Tailwind, and code Playground

by graphettion

JavaScript

// Requiremnts: Factors have actions. Actions are required to be completed. In order to complete an action, completedOn should be set to something other than null as null indicates it's not complete.

// hasEmptyFactors should be false and when added should be true
let factors1 = []

// hasEmptyActions should be false and when added should be true
let factors2 = [{
		actions: [],
    name: "factor1"
  },
  {
    name: "factor3"
  }
]

// hasEmptyCompletions should be false and when added should be true
let factors3 = [
  {
		actions: [{
      name: 'action1',
      completedOn: 'test'
    },{
    	name: 'action2',
      completedOn: 'test'
    }],
    name: 'factor1'
  },
  {
  	actions: [{
      name: 'action1',
      completedOn: 'test'
    },{
    	name: 'action2',
      completedOn: 'test'
    }]
  },
  {
    name: 'factor3',
    actions: [{
      name: 'action1',
      completedOn: 'test'
    },{
      name: 'action1',
      completedOn: 'test'
    }
    ]
  }
]

let hasEmptyFactors = !factors3.length // Good
let hasEmptyActions = factors3.some(factor => factor.actions && !factor.actions.length) // Good
let hasEmptyCompletions = factors3.every(factor => factor.actions && factor.actions.every(action => action.completedOn !== null)) // Bad

console.log('hasEmptyFactors', hasEmptyFactors)
console.log('hasEmptyActions', hasEmptyActions)
console.log('hasEmptyCompletions', !hasEmptyCompletions)