0

This addtoTaskList function needs to split the received task into 2 arrays(?) or two tasks split by a comma, then concatenate them and add them to the tasks array. As the code is currently, it outputs the split values to the taskList, but also outputs a non-split duplicate and clears the task list after every entry as shown below: Duplicate Output

I mostly need help with the concatenation, I think-thanks!

"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("\n");
    }
    // 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 task to array and local storage
        var partsOfStr = task.value.split(',');
        tasks = partsOfStr.concat(task.value);

        localStorage.tasks = tasks.join("|");

        // 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();
};
2
  • Though not necessary, I recommend you use JSON.stringify(tasks) and JSON.parse(storage) instead of joining and splitting your array with "|" characters. That way if someone enters a "|" when adding a task, it doesn't add extra values to your tasks array. Commented Nov 2, 2018 at 3:26
  • Thank you!!! So sorry for wasting your time :( U are my hero man Commented Nov 2, 2018 at 3:30

1 Answer 1

1

You are trying to concatenate an array (partsOfStr) with a string (task.value). Perhaps you meant to use tasks instead of task.value?

tasks = partsOfStr.concat(task.value);

Should be:

tasks = tasks.concat(partsOfStr);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Jason, unfortunately I tried that previously, and it results in no output...
@JamesHolloway I can't seem to reproduce your issue. Are you making sure to assign the result of the concat() operation to tasks? concat() does not modify the original array, it simply returns a new concatenated array.
Oh my Lord Jason, I am so sorry man, your solution worked! I had a capitalization error-oof!! thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.