locked
Cannot Create And Call Swap Function RRS feed

  • Question

  • User1928065626 posted

    I tried to create a Swap function and call it inside a Sort function but it is not working. Please see my code below.

    $(document).ready(function(){

    var myArray = [18,3,90,25,2,27,36, 22, 4, 77, 15, 9, 43, 54, 11, 5];
    var testString = '';

    function Swap(a, b){ var temp; temp = a; a = b; b = temp; } function Sort(array){ for(var j = 0; j< array.length-1; j++){ for(var k = 1; k< array.length; k++){ if(array[k] < array[k - 1]){ Swap(array[k] , array[k - 1]); } } } }

    Sort(myArray);

    for(var i = 0; i < myArray.length; i++){
    testString += ", " + myArray[i];
    }

    alert(testString.substring(2));

    })

    If I perform the swap inside of the for loop of the Sort() function instead of calling to the Swap() function to perform the swap, everything works fine. 

    I just want to know why my call to the Swap() function is not working.

    Monday, October 24, 2016 4:18 AM

Answers

  • User1928065626 posted

    I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2016 8:23 AM
  • User-6180675 posted

    I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.

    I have posted an easier way for this, you were just passing the values of array, not the actual array itself . Hence the values were not persisted in the array after swapping.

    Check my code below , I have posted 2 different ways to do that . The second one is neat but bit of risky to play with other library involved.

    Method 1 :

    //Modify your Swap function like below 
    
    //I am passing the position as well as the array itself
    function Swap(arr,a, b){
       var temp;
       temp = arr[a];
       arr[a] = arr[b];
       arr[b] = temp;
    }
    
    //Then call it from Sort function like below
    
    function Sort(array){
     for(var j = 0; j< array.length-1; j++){
         for(var k = 1; k< array.length; k++){
           if(array[k] < array[k - 1]){
             Swap(array,k, k - 1);  // pass the actual array and the position
           }
         }
     }
    }
    
    

    Method 2 :

    Add the Swap method to the builtin Array prototype , be careful

    //Add the method to the prototype
    
    Array.prototype.Swap = function (x,y) {
      var b = this[x];
      this[x] = this[y];
      this[y] = b;
      return this;  // return the current array instance
    }
    
    //Call it from the code like below
    
    function Sort(array){
     for(var j = 0; j< array.length-1; j++){
         for(var k = 1; k< array.length; k++){
           if(array[k] < array[k - 1]){
             array.Swap(k,k-1)  //Call it like this way
           }
         }
     }
    }

    Let me know if this works for you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2016 8:40 AM

All replies

  • User1928065626 posted

    I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2016 8:23 AM
  • User2009744128 posted

    Hi madjester,

    So if you do the swapping method inside the for loop then only it will work, because the swap method is only swapping the values of two variables it is not rearranging the values in the array, which is what you are trying to achieve via sorting.

    So do the sorting like this:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    <script>
    
    
    var myArray = [18,3,90,25,2,27,36, 22, 4, 77, 15, 9, 43, 54, 11, 5];
    var testString = '';
    
    function Swap(a, b){
       var temp;
       temp = a;
       a = b;
       b = temp;
    }
    
    function Sort(array){
     for(var j = 0; j< array.length-1; j++){
         for(var k = 1; k< array.length; k++){
           if(array[k] < array[k - 1]){
           //  Swap(parseInt( array[k]) ,parseInt( array[k - 1]));
            var temp=array[k];
            array[k]=array[k-1];
             array[k-1]=temp;
           }
         }
     }
    }
    
    Sort(myArray);
    
    for(var i = 0; i < myArray.length; i++){
      testString += ", " + myArray[i];
    }
    
    alert(testString.substring(2));
    
    
    </script>
    </head>
    <body>
    
    </body>
    </html>

    Hope this answers your question.

    Monday, October 24, 2016 8:33 AM
  • User-6180675 posted

    I've found the answer, it appears JavaScript parameters are always pass-by-value. There are ways to get around this but it's just too much work.

    I have posted an easier way for this, you were just passing the values of array, not the actual array itself . Hence the values were not persisted in the array after swapping.

    Check my code below , I have posted 2 different ways to do that . The second one is neat but bit of risky to play with other library involved.

    Method 1 :

    //Modify your Swap function like below 
    
    //I am passing the position as well as the array itself
    function Swap(arr,a, b){
       var temp;
       temp = arr[a];
       arr[a] = arr[b];
       arr[b] = temp;
    }
    
    //Then call it from Sort function like below
    
    function Sort(array){
     for(var j = 0; j< array.length-1; j++){
         for(var k = 1; k< array.length; k++){
           if(array[k] < array[k - 1]){
             Swap(array,k, k - 1);  // pass the actual array and the position
           }
         }
     }
    }
    
    

    Method 2 :

    Add the Swap method to the builtin Array prototype , be careful

    //Add the method to the prototype
    
    Array.prototype.Swap = function (x,y) {
      var b = this[x];
      this[x] = this[y];
      this[y] = b;
      return this;  // return the current array instance
    }
    
    //Call it from the code like below
    
    function Sort(array){
     for(var j = 0; j< array.length-1; j++){
         for(var k = 1; k< array.length; k++){
           if(array[k] < array[k - 1]){
             array.Swap(k,k-1)  //Call it like this way
           }
         }
     }
    }

    Let me know if this works for you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2016 8:40 AM
  • User1928065626 posted

    Hi, thanks for replying. Your solution works but I guess  the way your Swap function is called and what are passed in as arguments are a little different than what I expect.

    The following is an example I've found that calls the Swap function and pass in arguments the way I would expect arguments to be passed in.

    http://stackoverflow.com/questions/18817810/is-it-possible-to-write-a-numeric-swap-function-in-javascript

    Monday, October 24, 2016 3:45 PM