[2015-06-15] Challenge #218 [Easy] To-do list (Part 1)

Pretty easy JS answer:

Added an importantAdd(); function to put an item at the front of the list for a little extra challenge.

Comments and criticism welcome!!!

// Functions to call: // addItem(); // deleteItem(); // viewList(); // importantAdd();

var toDo = [];

//=============================== ADDING ITEM ==============================//

function addItem() {

    var item = prompt("What would you like to add to your to-do list?");

    toDo.push(item.toLowerCase());

    // PLAYING WITH HOW TO GET THE TO DO LIST NUMBERED

    for (var i = toDo.length - 1; i < toDo.length; i ++) {

        toDo[i] = (i + 1) + ") " + toDo[i];
    }

    return viewList();
}


//============================ ADD IMPORTANT ===============================//

// Need to place new item at front and re-number all the subsequent items

function importantAdd () {

    var item = prompt (" what would you like to add to the top of your list?");

    toDo.unshift(item.toLowerCase());

    for (var i = 0; i < toDo.length; i ++) {

        // Normal addItem(); function for the new first item

        if (i === 0) {

            toDo[i] = (i + 1) + ") " + toDo[i];

        } else { // RE-NUMBER SUBSEQUENT ITEMS BY GETTING RID OF THE "#) " and replacing it with the new (i + 1) value.

            toDo[i] = (toDo[i].slice(3, toDo[i].length));

            toDo[i] = (i + 1) + ") " + toDo[i];
        }
    }
    return toDo;
}

//=============================  DELETE ITEM  =============================//

function deleteItem() {

    var item = prompt("What have you completed?");

    for (var i = 0; i < toDo.length; i ++) {

        if (checkList(item)){

            if(toDo[i] === toDo[toDo.length]) {}
            toDo.splice(i, 1);

            return viewList();
        }
    }

    return "That's not on your to-list!";
}


//==============================  VIEW LIST  ================================//

function viewList() {

    if (toDo.length < 1 ) {

        return "You don't have anything on your to-do list!";

    } else {

        for (var i = 0; i < toDo.length; i ++) {

            console.log(toDo[i]);
        }
    }
}


//=============================  CHECK LIST  ===============================//

// checking current toDo array to see if an item is there.  
// This works nicely with the numbered return that addItem(); creates.

function checkList(item) {

    var isThere = false; 

    for (var i = 0; i < toDo.length; i ++) {

        if(item === (toDo[i].slice(3, toDo[i].length))) {

            isThere = true;
        }
    }

    return isThere;
}
/r/dailyprogrammer Thread