Question
Use the split method of the string object to convert the user's entry into an array. If there is more than one task in the
Use the split method of the string object to convert the user's entry into an array. If there is more than one task in the entry, use the concat method of the tasks array to add the new tasks to the tasks array. This is what I have been working with.
"use strict"; var $ = function(id) { return document.getElementById(id); };
var tasks = [];
var displayTaskList = function() { var list = ""; // if there are no tasks in tasks array, check storage if (tasks.length === 0) { // get tasks from storage or empty string if nothing in storage var storage = localStorage.getItem("tasks") || "";
// if not empty, convert to array and store in global tasks variable if (storage.length > 0) { tasks = storage.split("|"); } } // if there are tasks in array, sort and create tasks string if (tasks.length > 0) { tasks.sort(); list = tasks.join(" "); } // display tasks string and set focus on task text box $("task_list").value = list; $("task").focus(); };
var addToTaskList = function() { var task = $("task"); if (task.value === "") { alert("Please enter a task."); } else { // add tasks to array and local storage var newTasks = tasks.split(","); var nameTasks = newTasks.concat(","); alert (nameTasks.length); alert (nameTasks);
// clear task text box and re-display tasks task.value = ""; displayTaskList(); } };
var clearTaskList = function() { tasks.length = 0; localStorage.tasks = ""; $("task_list").value = ""; $("task").focus(); };
window.onload = function() { $("add_task").onclick = addToTaskList; $("clear_tasks").onclick = clearTaskList; displayTaskList(); };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started