How to stop repeating code

Hi guys , My function won’t run it say todos is undefined please help I was trying to create a function so I dont have repetetive code checkstorage() doesnt wanna run;Todo List

function checkStorage() {
    let todos;


    if (localStorage.getItem('todos') === null) {
        todos = [];
    } else {
        todos = JSON.parse(localStorage.getItem('todos'));
    }
}


function savelocalTodo(todo) {
    let todos;

    checkStorage();
    todos.push(todo);
    localStorage.setItem('todos', JSON.stringify(todos))

}

function loadTodo() {
    checkStorage();

    todos.forEach(
        function (a) {
            const todoDiv = document.createElement('div');
            todoDiv.classList.add('todo');

            //CREATE LI
            const newTodo = document.createElement('li');
            newTodo.innerText = a;
            newTodo.classList.add('todo-item');
            todoDiv.appendChild(newTodo);




            //CHECKED BTN
            const completeButton = document.createElement('button');
            completeButton.classList.add('complete-btn');
            completeButton.innerHTML = '<i class="fas fa-check"><i>';
            todoDiv.appendChild(completeButton);
            //DELETE BTN
            const trashButton = document.createElement('button');
            trashButton.classList.add('trash-btn');
            trashButton.innerHTML = '<i class="fas fa-trash"><i>';
            todoDiv.appendChild(trashButton);


            //ATTATCH TO UL LIST
            todoList.appendChild(todoDiv);
        }
    );
}


function deleteStorage(task) {
    let todos;
    checkStorage();
    let taskIndex = task.children[0].innerText;
    todos.splice(todos.indexOf(taskIndex), 1);
    localStorage.setItem('todos', JSON.stringify(todos))

}[https://mculo-fish.github.io/tasklist/?todos=all](https://mculo-fish.github.io/tasklist/?todos=all)

It’s this line throwing the error, because todos is not defined in this function.

it’s not working im trying to run the function checkStorage() inside savelocalTodo(todo) & loadTodo() & deleteStorage(task)

change that line to
if (localStorage.getItem('todos') === undefined) {
OR
if (!localStorage.getItem('todos')) {

It doesnt work too I dont know anymore

On your code, you declared the variables, but didn’t assign it a value. Yeah, you did assign it in the checkStorage() function, but you didn’t return it, which means the value is only kept inside the checkStorage() function. I’ll put comments explanations and arrows on this code below:

function checkStorage() {
    let todos;
        ↑//you declared it here it is only declared inside 
         //the checkStorage() function

    if (localStorage.getItem('todos') === null) {
        todos = [];
        ↑//this will only change the value inside this function
    } else {
        todos = JSON.parse(localStorage.getItem('todos'));
        ↑//this will only change the value inside this function
    }
} ←//after this, todos will be undefined, because you didn't
//return its value


function savelocalTodo(todo) {
    let todos;
        ↑//you declared the variable without value

    checkStorage();
    todos.push(todo);
    ↑//then you're trying to push into something that has no value
    localStorage.setItem('todos', JSON.stringify(todos))

}

A simple fix would be to declare a variable outside all of the function, that will store the todos, then on the functions, you can just use the return the variable with a value.

1 Like

Thank you it works perfectly without me returning the value I only declared a single variable outside.

Glad to know I helped. Now you can go back to your project and finish it with pride.
Happy Coding!! :slightly_smiling_face:

1 Like

Thanks Alot Budd I hope I dont run into another error

1 Like