Web Analytics Made Easy -
StatCounter Copying the contents between 2 arrays with some processing -- help needed. - CodingForum

Announcement

Collapse
No announcement yet.

Copying the contents between 2 arrays with some processing -- help needed.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Copying the contents between 2 arrays with some processing -- help needed.

    Hello,

    I am new to this forum. I am learning javascript and I have a programming problem.

    I am trying to copy the contents between 2 arrays.

    There is a XHTML UI that presents the user with the ability to add work. Each work he adds, will get displayed in a textarea, in a new line (numbered).

    There is a button that the user can click, should he want to promote a later task to number 1 position in the text area.

    If he does that, I assign this selected index value to a variable.

    I copy the existing array to a new array intact.

    Then I manipulate in the following manner:

    Suppose, array 1 has the following values - ["Study", "bathe", "cook", "sleep"].

    If the user wanted to promote "cook" to position 1,

    then this array will be copied 1st to a new array. This wil make "sleep" occupy the last position in both arrays.

    Then I will copy "bathe" from old array to position 3 ("cook" position) of new array, "study" into position 2 ("bathe" position) of new array, and then finally will copy "cook" from new array into position 0 of old array using the saved index that user entered.

    When I coded for the above condition, my code does not work. Can someone help me find where is the problem?

    Code:
    var promote_task_list = function () {
    	var selectedTask = parseInt( prompt("Please enter the task number of the task you want to promote"));
        
    	dup_array = task_list;
    
    	if (selectedTask == 0) {
    		alert("Selected task to promote already in 1st place, nothing to promote");
    	} else {
    		for (var i = selectedTask; i > 0; i--) {
    		    task_list[i]  = dup_array[i - 1];
    	    } 
    		task_list[i] = dup_array[--selectedTask];
    	}
    	
    	update_task_list(); 
    }
    Thank you.

  • #2
    The problem is that this:
    Code:
    dup_array = task_list;
    doesn't really create a copy of the array but a reference only. Every action you perform on task_list is visible in dup_array and vice versa. To create a copy you have to clone the array:
    https://davidwalsh.name/javascript-clone-array
    This works:
    Code:
                var task_list = ["Study", "bathe", "cook", "sleep"], dup_array;
                var promote_task_list = function () {
                    var selectedTask = parseInt(prompt("Please enter the task number of the task you want to promote"));
    
                    dup_array = task_list.slice(0);
    
                    if (selectedTask == 0) {
                        alert("Selected task to promote already in 1st place, nothing to promote");
                    } else {
                        for (var i = selectedTask; i > 0; i--) {
                            task_list[i] = dup_array[i - 1];
                        }
                        task_list[i] = dup_array[selectedTask];
                    }
                }
                promote_task_list();
                console.log(task_list);
    However, in order to perform this modification on the array it's not necessary to create a copy. This works too:
    Code:
                var promote_task_list = function () {
                    var selectedTask = parseInt(prompt("Please enter the task number of the task you want to promote"));
    
    
                    if (selectedTask == 0) {
                        alert("Selected task to promote already in 1st place, nothing to promote");
                    } else {
                        var tmp = task_list[selectedTask];
                        for (var i = selectedTask; i > 0; i--) {
                            task_list[i] = task_list[i - 1];
                        }
                        task_list[i] = tmp;
                    }
                }
    Last edited by Sempervivum; Oct 3, 2016, 06:18 PM.

    Comment


    • #3
      Hi Sempervivum,

      Thank you! Your solution #2 that copies within the same array (I coded originally that way, but changed it to having 2 arrays later because the same array solution did not work for me).

      I had to decrement the selectedTask variable in the given solution #2.

      Now my code looks like this and this works.

      Code:
      var promote_task_list = function () {
      	var selectedTask = parseInt( prompt("Please enter the task number of the task you want to promote"));
        
      	if (selectedTask == 0) {
      		alert("Selected task to promote already in 1st place, nothing to promote");
      	} else {
      		selectedTask--;
      		var tmp = task_list[selectedTask];
              for (var i = selectedTask; i > 0; i--) {
                  task_list[i] = task_list[i - 1];
              }
              task_list[i] = tmp;
      	}
          update_task_list(); 
      }
      Thank you so much!

      Comment

      Working...
      X