I try to make a simple to do list but when I put let shoppingList = []; inside addItem() function it's not working. Why this is happening?
let shoppingList = [];
function addItem() {
let Item = document.getElementById("item").value;
let output = document.getElementById("output");
let html = '';
if (shoppingList.indexOf(Item) === -1) {
shoppingList.push(Item);
}
for (var x = 0; x < shoppingList.length; x++) {
html += (x + 1) + ". " + shoppingList[x] + "<br>";
}
output.innerHTML = html;
}
let shoppingList = [];inside the function you'll get a new empty list every time the function is called. It sounds like that's not what you want.