Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
How to insert an array to another array at each iteration of a for loop in javascript

I have a function to bubble sort and I want to save the array after each swap into another array. The bubble sort is working properly and I can log the array after each swap to the console. But I cant seem to push to the other array properly.

Here's my code :

var arr = [5, 4, 3, 2, 1];
var steps = [];

function bubbleSort() {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        swap(arr, j, j + 1);
      }
      var temp = arr;
      console.log(temp);
      steps.push(temp);
    }
  }
  console.log(steps);
}

const swap = (a, x, y) => {
  var temp = a[x];
  a[x] = a[y];
  a[y] = temp;
};

bubbleSort();

Here's a screenshot of the console :

screenshot of console

It's only when I try to use get a hold of the array at each step that it isn't showing properly? what do I do?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think clonning the array could work ? var temp = [...arr];

var arr = [5, 4, 3, 2, 1];
var steps = [];

function bubbleSort() {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        swap(arr, j, j + 1);
      }
      var temp = [...arr];
      console.log(temp);
      steps.push(temp);
    }
  }
  console.log(steps);
}

const swap = (a, x, y) => {
  var temp = a[x];
  a[x] = a[y];
  a[y] = temp;
};

bubbleSort();

about 4 years ago · Santiago Trujillo Report

0

You are inserting the Reference of the Temp-Array and not the actual content. This way, you store multiple times the reference and at the End you are presented with all those reference pointing to the last version of your temp Array.

You can use the destructuring assignment of an array, to create an easy shallow copy to be stored.

var arr = [5, 4, 3, 2, 1];
var steps = [];

function bubbleSort() {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        swap(arr, j, j + 1);
      }
      var temp = arr;
      console.log(temp);
      // Changed to destructuring assignment
      steps.push([...temp])
    }
  }
  console.log(steps);
}

const swap = (a, x, y) => {
  var temp = a[x];
  a[x] = a[y];
  a[y] = temp;
};

bubbleSort();

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!