Im so proud of myself :)

Posted by goldenzftw@reddit | learnprogramming | View on Reddit | 5 comments

im so happy right now and i want to share what happened with you guys. So recently i was working on a to-do list in JavaScript to learn how to work with localstorage and after looking up docs and youtube videos online i finally made the ADD and delete functions work but with one critical flaw. The delete function was made in such a way that it got rid of all entries that matched its parameters but just the one you want to get rid of here's the code: d.addEventListener("click", () => {

let p = JSON.parse(localStorage.getItem("list")) || [];

///////////////////////////////////////////////////////////

//p = p.filter((t) => t !== current);

//localStorage.setItem("list", JSON.stringify(p));

//console.log("localstorage when delete: ", localStorage);

//list.removeChild(li);

//////////////////////////////////////////////////////////

i tried to fix the flaw but i was too tired to make it work. Fast forward the next day and i was in the bathroom contemplating when an algorithm came to me. i went to my computer wrote it down and it worked!. heres the code:
p.forEach((t) => t == current);

p.pop(current);

localStorage.setItem("list", JSON.stringify(p));

console.log("localstorage when delete: ", localStorage);

list.removeChild(li);