The issue in your code when trying to useforEachis in how you’re concatenating theoutputvariable.forEachtakes a callback function that provides the element, the index, and the array. You’re overwritingoutputin each iteration, which isn’t correct becauseoutputshould be accumulating the HTML content. Let's correct this:
Here’s how you can useforEachcorrectly:
Updated Code:
────────────────────────────────────────────────────────────────────
javascriptCopy codefunction () {
let displaytodo = document.getElementById('displayloops').value.trim();
let datetime = document.getElementById('date-time').value.trim();
if (displaytodo && datetime) {
todolists.push(, datetime });
localStorage.setItem('todolists', ));
showCustomAlert('Successfully saved');
}
let output = '';
// Use forEach to iterate over the todolists array
todolists.forEach(function(todolist, i) {
output += <div>${i + 1}. ${todolist.displaytodo} - ${todolist.datetime} <button onclick="dele(${i})">Delete</button></div>;
});
// Display the output or a message if no items are present
document.getElementById('loppsTodolist').innerHTML = output || 'No items in the list.';
// Clear input fields
document.getElementById('displayloops').value = '';
document.getElementById('date-time').value = '';
}
function showCustomAlert(message) {
const alertBox = document.getElementById('customAlert');
alertBox.textContent = message; // Set the message
alertBox.style.display = 'block'; // Make the alert visible
alertBox.style.opacity = '1'; // Ensure it starts fully visible
// Fade out after 3 seconds
(() => {
alertBox.style.opacity = '0'; // Start fading out
setTimeout(() => {
alertBox.style.display = 'none'; // Hide the alert after fading
}, 500); // Match this with the CSS transition duration
}, 3000); // Alert visible for 3 seconds
}
function dele(indexe) {
const deletedis = todolists[indexe].displaytodo;
if (confirm(Are you sure you want to delete this item: "${todolists[indexe].displaytodo}" with time "${todolists[indexe].datetime}"?)) , 1);
localStorage.setItem('todolists', ));
showCustomAlert(Successfully deleted ${deletedis});
();
}
}
Explanation of Changes:
UsingforEach:TheforEachloop is used to iterate over thetodolistsarray. It correctly generates the HTML string for each todo item and appends it to theoutputvariable.TheforEachcallback function receives two arguments:todolist(the current item) andi(the index).Theoutputstring accumulates HTML for each item in the list.
TheforEachcallback function receives two arguments:todolist(the current item) andi(the index).
Theoutputstring accumulates HTML for each item in the list.
Displaying the Output:Thedocument.getElementById('loppsTodolist').innerHTMLnow receives the complete HTML string fromoutput.
UsingforEach:TheforEachloop is used to iterate over thetodolistsarray. It correctly generates the HTML string for each todo item and appends it to theoutputvariable.
TheforEachcallback function receives two arguments:todolist(the current item) andi(the index).
Theoutputstring accumulates HTML for each item in the list.
TheforEachcallback function receives two arguments:todolist(the current item) andi(the index).
Theoutputstring accumulates HTML for each item in the list.
Displaying the Output:Thedocument.getElementById('loppsTodolist').innerHTMLnow receives the complete HTML string fromoutput.
WhyforEachWorks Here:
forEachis designed for iterating over arrays and performing a side-effect (like building up a string) in each iteration.
Theoutputvariable is accumulated in each iteration, so when the loop finishes, it holds the entire HTML structure to display all todos.
forEachis designed for iterating over arrays and performing a side-effect (like building up a string) in each iteration.
Theoutputvariable is accumulated in each iteration, so when the loop finishes, it holds the entire HTML structure to display all todos.
This should work correctly and allow you to useforEachto generate your todo list.