Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    61
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to get rid of list number when using the unshift method

    So im working on code that user adds things to a list. When you hit the promote button, the user chooses which task to promote to the beginning by way of the unshift method. My problem that I can't figure out is how to get rid of the number to the old task that I promoted. It's like its an empty string. I'm just looking for some ideas to how I might go about solving it. The first code is the html, and the second is the .js.

    Code:
    <body>
    <div id="content">
        <h1>Task List Manager</h1>
        <p id="tasks"><textarea id="task_list" rows="5" cols="50"></textarea></p>
        <p><input type="button" name="add_task" id="add_task"
            value="Add Task" /></p>
        <p><input type="button" name="modify_task" id="modify_task"
            value="Modify Task" /></p>
        <p><input type="button" name="delete_task" id="delete_task"
            value="Delete Task" /></p>
    	<p><input type="button" name="promote_task" id="promote_task"
            value="Promote Task" /></p>
    </body>
    Code:
    var task_list = [];
    
    var $ = function (id) { return document.getElementById(id); }
    
    var update_task_list = function () {
        if ( task_list.length == 0 ) {
            $("task_list").value = "";
        } else {
            var list = "";
            for ( var i in task_list ) {
                list += (parseInt(i)+1) + ": " + task_list[i] + "\n";
            }
            $("task_list").value = list;
        }
    	
    	
    }
    
    var add_task_click = function () {
        $("add_task").blur();
        var task = prompt("Enter a task:", "");
        if ( task != "" && task != null) {
            task_list.push(task);
            update_task_list();
        }
    }
    
    var delete_task_click = function () {
        $("delete_task").blur();
        if ( task_list.length == 0 ) {
            alert("No task to delete.");
            return;
        }
        var to_delete = prompt("Enter the task number to delete:", "");
        if (to_delete == null) { return; }
        to_delete = parseInt(to_delete);
        if ( isNaN(to_delete) ) {
            alert("You did not enter a number.");
            return;
        }
        if ( to_delete < 1 ) {
            alert("The task number is too low.");
            return;
        }
        if ( to_delete > task_list.length ) {
            alert("The task number is too high.");
            return;
        }
        to_delete--;
    	task_list.shift();
        //task_list.splice(to_delete, 1);
        update_task_list();
    }
    
    var modify_task_click = function() {
    var mod = parseInt(prompt("Enter the task number that you choose to modify:", "") );
    var wow = parseInt(mod - 1);
    var bomb = prompt("Modify your task",task_list[wow]);
    task_list[wow] = bomb;
    update_task_list();
    }
    
    var promote_task_click = function ()  {
    var mud = parseInt(prompt("Enter the task number that you choose to be promoted:", "") );
    var mooh = parseInt(mud - 1);
    var fat = task_list[mooh];
    task_list.unshift(fat);
    var cops = parseInt(mooh + 1);
    task_list[cops] = "";
    update_task_list();
    }
    
    
    window.onload = function() {
        $("add_task").onclick = add_task_click;
        $("delete_task").onclick = delete_task_click;
    	$("modify_task").onclick = modify_task_click;
    	$("promote_task").onclick = promote_task_click;
        update_task_list();
    }

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Quote Originally Posted by krisdeniseriley View Post
    The first code is the html, and the second is the .js.
    he,he, if someone would have to read that to figure what is in boxes, wouldn't know anything about it anyway (-:

    Better provide link to sample page, then someone will likely help you.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by krisdeniseriley View Post
    It's like its an empty string.
    It's not only like it's an empty string, it is an empty string, and that's because you make it so right here:
    PHP Code:
    task_list[cops] = ""
    Setting an array element to an empty string doesn't remove it from the array, of course. If you want to completely remove it, the array has to be spliced, so the remaining elements will be renumbered:

    PHP Code:
    var promote_task_click = function ()  {
        var 
    taskNumber parseInt(prompt("Enter the task number that you choose to be promoted:""") );
        var 
    taskIndex taskNumber 1;
        var 
    task task_list.splice(taskIndex1)[0];
        
    task_list.unshift(task);
        
    update_task_list();
    }; 
    Notice how I used descriptive variable names. You can do whatever you want in your own code, but if other people are supposed to work on your code — and asking a question in a forum means you're actually asking other people to do just that — silly variable names are very annoying.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    61
    Thanks
    3
    Thanked 0 Times in 0 Posts

    I appreciate your insite

    I agree with you, and I will definitely take your advice an variable names. I do see how the splice method was important in accomplishing what I was looking for. I appreciate your time and logic to help solve my hair pulling. Thanks again.

    Kris


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •