//set local storage item (must be passed as strings)
// localStorage.setItem('name', 'John');
// localStorage.setItem('age', '30');
//set session storage item
// sessionStorage.setItem('name', 'Beth');
//remove from storage
// localStorage.removeItem('name');
//get from storage
// const name = localStorage.getItem('name');
// const age = localStorage.getItem('age');
// //clear local storage
// localStorage.clear();
// console.log(name, age);
document.querySelector('form').addEventListener('submit', function(e){
const task = document.getElementById('task').value;
let tasks;
if(localStorage.getItem('tasks') === null){
tasks = [];
} else {
tasks = JSON.parse(localStorage.getItem('tasks'));
}
tasks.push(task);
localStorage.setItem('tasks', JSON.stringify(tasks));
alert('Task saved');
e.preventDefault();
});
const tasks = JSON.parse(localStorage.getItem('tasks'));
tasks.forEach(function(task){
console.log(task);
})
//JSON.parse takes string data and makes it usable as an
//array with array methods such as forEach.
//Remember that we must use strings when passing
//things to localStorage and then convert
//them back when necessary using JSON.parse