Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Improving the Application Code Refactor 2: Readable Branching Logic

Made my own version of refactoring, can you please rate my sheet and i'm open for suggestions,criticism

const form = document.getElementById('registrar');
const input = document.querySelector('form > input[type=text]');
const mainDiv = document.querySelector('.main');
const ul = document.getElementById('invitedList');
const div = document.createElement('div');
const label = create('label','textContent','Hide unconfirmed guests');
const cBox2 = create('input','type','checkbox');

function create(elem,property,value) {
  const element = document.createElement(elem);
  element[property] = value;
  return element;
}

function exec(obj,funct,arr) {
  for(let i = 0; i < arr.length; i++) {
    obj[funct](arr[i]);
  }
}

exec(div,'appendChild',[label,cBox2]);
mainDiv.insertBefore(div,ul);

cBox2.addEventListener('change',() => {
  const checked = cBox2.checked;
  const lis = ul.children;

  if(checked) {
    for(let i = 0; i < lis.length; i++){
      if(lis[i].className != 'responded') {
        lis[i].style.display = 'none';
      }
    }
  } else {
    for(let i = 0; i < lis.length; i++){
      lis[i].style.display = 'block';
    }
  }
});

function createList(invitee) {
  const li = document.createElement('li');
  const span = create('span','textContent',invitee);
  const label = create('label','textContent','Confirmed')
    .appendChild(create('input','type','checkbox')).parentNode;
  const editButton = create('button','textContent','edit');
  const removeButton = create('button','textContent','remove');

  exec(li,'appendChild',[span,label,editButton,removeButton]);
  return li;
}

form.addEventListener('submit',(e) => {
  e.preventDefault();
  const invitee = input.value;
  const newLi = createList(invitee); 
  input.value = '';

  exec(ul,'appendChild',[newLi]);
});


ul.addEventListener('change',(event) => {
  const checkbox = event.target;
  const check = event.target.checked;
  const li = checkbox.parentNode.parentNode;

  if(check) {
    li.className = 'responded';
  } else {
    li.className = '';
  }

});

ul.addEventListener('click',(e) => {
  if(e.target.tagName = 'BUTTON') {
    const button = e.target;
    const li = button.parentNode;

    function editConfig (element,setting) {
      const obj = {input: ['value','textContent'],span: ['textContent','value']};
      const remove = li.firstChild;
      const insert = create(element,obj[element][0],remove[obj[element][1]]);
      if(setting === 'save') {
        insert.type = 'text';
      }
      li.insertBefore(insert,remove);
      li.removeChild(remove);
      button.textContent = setting;
    }

    if(button.textContent == 'edit') {     
      editConfig('input','save');

    } else if(button.textContent == 'save') {      
      editConfig('span','edit');

    } else if (button.textContent == 'remove') {
      ul.removeChild(li);
    }
  }
});