Why has my input array changed?

Hi I need help with this code.

When I test this outside the function the return array is an updated version of the input array which is great. However I find that the input array has also changed in a way that it is the same as the return array but that should stay the same as it was before it was passed into the function.
Any help much appreciated.

function findIsolatedArrayPairs(arr){

	let myArray = [...arr];

	let subFunction = function(schema){
		for(let schematic in schema){
			let set = {};
			let isoPairsSet = {}
					set[schema[schematic][i]] = myArray[schema[schematic][i]];
				return true;

			function isoPairs(set){
				for(let cell in set){ 
						for(let cells in set){
				return Object.entries(isoPairsSet).length!=2 ? false:true;

			function isoPairReduce(IPS){
				let nums = Object.values(IPS)[0];
				for(let cell in set){
						if(set[cell].indexOf(nums[0])!=(-1)) {myArray[cell].splice(myArray[cell].indexOf(nums[0]),1);console.log(set[cell].indexOf(nums[0]));}
						if(set[cell].indexOf(nums[1])!=(-1)) {myArray[cell].splice(myArray[cell].indexOf(nums[1]),1);console.log(set[cell].indexOf(nums[1]));}

		return false;

	return subFunction(boxSchema) ? myArray:subFunction(colSchema)?myArray:subFunction(rowSchema)?myArray:myArray;


Is this for a specific FCC challenge? Do you have a test array we could use? What is it supposed to do?

Without tracing your code or any input data I would suspect that you are not making a deep copy of your input array

alhazen1 makes a good point.

You make a copy of arr with

let myArray = [...arr];

But that only makes a new copy of arr. The any value inside arr that is not a primitive will still point to the same place in memory. For example, if you send in:

const inputArray = {
  num: 127,
  name: {
    first: 'Bob',
    last: 'Barker'

So, when you make a copy, any changes made to newArr.num will not affect the original. And if you reassign newArr.name, then that won’t affect the original either. But because newArr.name starts out with the same address that inputArray.name had, any change to newArr,name.first will affect the original.

Yes, it’s confusing. It’s the difference between a shallow copy and deep copy. You can google it and find out. It is a complicated subject. I always think I had it easy because I learned C first, where understanding this is fundamental to the language. But it can be learned.

1 Like

Dear Kevin and alhazen1,

thank you for responding. This is something I kind of knew/heard about (deep vs shallow copies). After presenting this problem and reading your replies there has been a few light bulbs. Thank you so much. I will now go away and play around with the code given my new insights. I think this may well be the reason for my problem.

My code is part of an application that solves sudoku puzzles (just for fun). The application in all of its raw form is at https://morrellj.github.io/sudoku.html . It does not have the code I am having issues with committed to it just yet and, after your posts, I realise it is a bit dirtier than I first thought.

Thanks again