0

I have the following code below, the value that is being passed in is 4, 1, 9, 14, 6 ,8 and the value which is assigned to newHeight is 1, 4, 6, 8, 9, 14. Insertion Sort sorts the array in ascending order.

var heightChecker = function(heights) {

    var sorted = [...heights];
    var newHeight = insertionSort(heights);
    var count = 0;
    
    for(var i = 0; i < newHeight.length; i++) {
        if(newHeight[i] !== sorted[i]) {
            count++;
        }
    }

    return count;
}

insertionSort sorts the array, and when i use this line of code

var sorted = [...height];

Then it returns the answer I was looking for which is 3. However when i change the code to be


 var heightChecker = function(heights) {

      var newHeight = insertionSort(heights);
      var count = 0;
      
      for(var i = 0; i < heights.length; i++) {
          if(newHeight[i] !== heights[i]) {
              count++;
          }
      }
  
      return count;
  }

It returns the answer as 0.

I am not understanding why it isn't the same answer, and after debugging and google searching, I still cannot find why.

Here is the insertion sort code

function insertionSort(inputArr) {
    let n = inputArr.length;
    for (let i = 1; i < n; i++) {
        // Choosing the first element in our unsorted subarray
        let current = inputArr[i];
        // The last element of our sorted subarray
        let j = i-1;
        while ((j > -1) && (current < inputArr[j])) {
            inputArr[j+1] = inputArr[j];
            j--;
        }
        inputArr[j+1] = current;
        console.log(inputArr);
    }
    return inputArr;
}
2
  • Could you also post back code from insertionSort function Commented Sep 17, 2020 at 4:27
  • just added the insertionSort function code Commented Sep 17, 2020 at 5:27

1 Answer 1

1

a = [...b] creates a copy of b, a = b only assigns a different name to your value (i.e. a second reference pointing to the same value).

let a = [1,2,3];
b = a;
c = [...a];
a[1] = 41; // modifies value of a AND b
b[1] = 42; // modifies value of a AND b
c[1] = 43; // only modifies value of c
console.log(a); // 1, 42, 3
console.log(b); // 1, 42, 3
console.log(c); // 1, 43, 3

or, with a function call:

function addNumber(array, number) {
  array.push(number);
}
let a = [1,2,3];
b = a;
c = [...a];
addNumber(b, 4); // now a = [1,2,3,4]; and b = [1,2,3,4] (=a); c = [1,2,3]
addNumber(c, 5); // still a = [1,2,3,4]; but c = [1,2,3,5]
Sign up to request clarification or add additional context in comments.

2 Comments

I just edited my code, With my new edits does your comment still hold true? Instead of assigning the parameter in the first function, I am just using the parameter in the loop.
I assume. Assigning an array to a diferent variable does not copy the array's contents, using the rest operator (or .slice) however does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.