bamsi/list-structure

Javascript best practices

Opened this issue · 0 comments

bamsi commented

I have checked the javascript best practices and I have found the following areas for improvement!

  • CSS - there is a repetition of some styles for the input.
    input[type="text"] {
    border: none;
    outline: none;
    height: 20px;
    padding: 1rem;
    width: 80%;
    }
    .input-text input:active {
    border: none;
    outline: none;
    }
  • Javascript - there is a repetition of the same item, can be declared as a const.
    function readLocalStorage() {
    let data = [];
    if (JSON.parse(window.localStorage.getItem('list'))) { data = JSON.parse(window.localStorage.getItem('list')); }
    return data;
    }
  • Javascript - there is a repetition of the same block, can be optimized to only one function.
    function update(item) {
    const items = readLocalStorage();
    const element = items.filter((i) => i.id === item.id)[0];
    element.description = item.description;
    items[item.id - 1] = element;
    writeLocalStorage(items);
    }
    function completed(index, status) {
    const items = readLocalStorage();
    const element = items.filter((i) => i.id === index)[0];
    element.completed = status;
    items[index - 1] = element;
    writeLocalStorage(items);
    }