...

View Full Version : How to get rid of list number when using the unshift method



krisdeniseriley
11-11-2011, 06:26 AM
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.


<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>


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();
}

BubikolRamios
11-11-2011, 09:10 AM
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.

venegal
11-11-2011, 02:24 PM
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:

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:


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(taskIndex, 1)[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.

krisdeniseriley
11-11-2011, 04:55 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum