How is it possible for this variable to be altered without any modifications made to it in the current function?

This particular function receives two arrays as input: arrOne, which is an array comprising arrays of numbers, and arrTwo, which is an array containing numbers. My goal here is to generate every possible combination, followed by obtaining the unique combinations. However, I've encountered a stumbling block.

function multOrdProduct(arrOne,arrTwo){
  let backSet = [];
  let count = 0;
  let tempArr = new Array(arrOne.length);
  let permArrOne = [];
  let permArrTwo = [];
  let pushed;
  let setPart = [];
  
  for(i=0; i < arrOne.length; i++){
    permArrOne.push(arrOne[i]);
  }
// ^sets permArrOne to equal the same array as ArrOne
  
  for(i = 0; i < arrOne.length;i++){
    for(j = 0; j < arrTwo.length; j++){

      setPart = permArrOne[i];
      
      console.log(permArrOne[i]);
      
      setPart.push(arrTwo[j]);
      backSet.push(setPart);
    }
  }
  return backSet;
}

I initially believed that permArrOne[i] would remain unchanged since no value assignment is made to it. Can anyone explain why it changes in each iteration?

Answer №1

When you reference permArrOne in setPart without cloning it, any changes made to setPart will also affect permArrOne.

.slice() is a method that can be used to create a new array:

function multiplyArrays(arr1,arr2){
  let backSet = [];
  let count = 0;
  let tempArr = new Array(arr1.length);
  let permArr1 = [];
  let permArr2 = [];
  let pushed;
  let setPart = [];
  
  for(i=0; i < arr1.length; i++){
    permArr1.push(arr1[i]);
  }
// ^sets permArr1 to be the same as arr1
  
  for(i = 0; i < arr1.length;i++){
    for(j = 0; j < arr2.length; j++){

      setPart = permArr1[i].slice();
      
      console.log(permArr1[i]);
      
      setPart.push(arr2[j]);
      backSet.push(setPart);
    }
  }
  return backSet;
}

let array1 = [[0, 1], [1, 2]]

let array2 = [0, 1, 2]

multiplyArrays(array1, array2)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refresh your webpage automatically without the need to manually refresh after clicking a button using AJAX in HTML and PHP!

One issue I'm facing is that the page doesn't auto-refresh, although it loads when I manually refresh it. Below you can find my HTML and AJAX code along with its database details. The Trigger Button <?php $data = mysqli_ ...

The error message pops up when a local variable is returned

This question has come up before, but I find it unusual to encounter the message - local variable df_ret referenced before assignment - when using this function to recalibrate imbalanced data sets: def down_sample(df, target, positive_label, negative_lab ...

Scan for every header tag present and verify the existence of an id attribute within each tag. If the id attribute is absent, insert

Looking to locate all header tags within the content and verify if each tag has an id attribute. If not, then jQuery should be used to add the id attribute. Here is the code snippet: var headings = $("#edited_content").find("h1,h2,h3,h4,h5,h6"); $.each( ...

React.js: The specified element type is not valid:

I am currently working on a sample project using react.js in Visual Studio 2019 Here is my Index.js file: import 'bootstrap/dist/css/bootstrap.css'; import React from 'react'; import ReactDOM from 'react-dom'; import { Provi ...

Guide to incorporating a jade file into a node.js application after executing an Ajax request

While working on my node.js application, I encountered an issue with loading a new HTML file using Ajax on a button click. Despite everything else working perfectly, the new HTML file was not being loaded. This is how I am making the ajax call in my main. ...

Learn how to retrieve specific user information from a JSON file by implementing a button click feature within a custom directory using Angular.js

I am just starting to learn angular.js and I'm trying to figure out how to retrieve JSON data from a custom directory. My goal is to display the information of a specific user when a particular button is clicked, but so far I haven't been success ...

What is the process for assigning a PHP function's return value to a JavaScript variable?

Is there a way to retrieve a value from a PHP function and assign it to a JavaScript variable? As shown in the following example: PHP: // Destination folder for downloaded files $date = date('m.d.y'); mkdir("uploads/" . $date, 0777, true); $ ...

Display a single video on an HTML page in sequential order

My webpage contains a video tag for playing online videos. Instead of just one, I have utilized two video tags. However, when attempting to play both videos simultaneously, they end up playing at the same time. I am seeking a solution where if I start pla ...

Angular Bootstrap notifications will stay open indefinitely and will not automatically dismiss after a certain period

I need help with adding alerts using Angular Bootstrap that should dismiss themselves after a certain timeout. However, the alerts are not dismissing on their own. Here's the code snippet: angular.module('ui.services').service('AlertSe ...

Implement a one-second delay before nesting another animation

I'm currently utilizing a timeout function, but I am interested in using a delay instead. My goal is to have the second animation start when one second has passed from the beginning of the first animation, and it should be a linear animation. How can ...

What is the best way to transfer information between two components in React.js by simply clicking a button?

One of the challenges I am currently facing involves rendering data from the Pokemon API in a table. Within this table, there is a button that allows users to select specific data when pressed. My goal is to send this selected data from the table component ...

What is the best way to calculate the time elapsed between two consecutive double clicks using jQuery?

I am trying to calculate the time difference between two clicks on a single button. Here is my code snippet: <a href="#">click here</a> My current JavaScript code to capture the time of each click is as follows: var clickedTime = '&apos ...

Display a div element using AngularJS

Looking for a way to display a div using AngularJS, I came across some solutions on StackOverflow. However, implementing them did not work in my case. Here is my HTML code: <div id="myPanel" ng-controller="controllerDependance" ng-show="myvalue" clas ...

unable to import React Component

As I embark on creating a simple example React project, my goal is to utilize the npm package drag and drop file picker found at: https://www.npmjs.com/package/@yelysei/react-files-drag-and-drop To begin, I initiated a fresh React project using npx create ...

Sort through an array of objects using the Filter API to find objects with matching

I need assistance in dynamically filtering my array based on a condition where values from one array match another static array. The array nfts is dynamic and changes depending on the user. It contains metadata, images, transaction information, etc. Speci ...

Utilize moment.js to convert an epoch date into a designated time zone

I've spent countless hours searching for a resolution to the issue with moment.js and its inability to accurately display the correct date for a given local time zone. Let me explain my predicament: The flight API I'm utilizing provides me w ...

Changing the border of an iframe element using jQuery or Javascript from within the iframe itself

Is it possible to set the border of an iframe to zero from within the iframe itself using JavaScript or jQuery? Any guidance on how this can be achieved would be greatly appreciated. Thank you! ...

The functionality of React setState seems to be malfunctioning when activated

Having encountered an unusual issue with react's setState() method. Currently, I am utilizing Azure DevOps extensions components and have a panel with an onClick event that is intended to change the state of IsUserAddedOrUpdated and trigger the addOr ...

Find and conceal the object within the list that includes the term "Ice"

The teacher's assignment is to create a radio button filter that hides items with the word "Ice" in them, such as "Ice Cream" and "Iced Tea." Here is the current code I have been working on: <!DOCTYPE html> <html> <head> <me ...

The text does not change in the input field even with the .value method

I'm encountering an issue where I want to use .value to set the text of an input field. However, after setting the value of the input field, the text disappears when clicked on. I would like to find a solution where the text does not disappear upon cl ...