-1

let arrays = [ [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ] ]

console.log(arrays);

for(i = 0; i < arrays.length; i++){
    arrays[i][(arrays.length - 1) - i] = '-'
}
console.log(arrays); //[ [ ' ', ' ', '-' ], [ ' ', '-', ' ' ], [ '-', ' ', ' ' ] ]

This is the result I want.

The code below has a different result.

let input = 6;

const diameter = input; //6
const centralValue = Math.ceil(diameter / 2); //3

const reverseArray = (array) => {return array.slice(0).reverse();} //어레이 뒤집어줌
// const makeUpperLeftArrays = function(centralValue){
// }

let createEmptyArray = (number) => {return new Array(number).fill(' ')} //빈 배열 생성

let emptyArray = createEmptyArray(centralValue);

let addEmptyArrays = function(array){
    let addedEmptyArrays = [];
    for(i = 1; i <= centralValue; i++){
        addedEmptyArrays.push(array)
    }
    return addedEmptyArrays;
}
let addedEmptyArrays = addEmptyArrays(emptyArray);


let arrays = addedEmptyArrays
//[ [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ] ]

console.log(arrays);

for(i = 0; i < arrays.length; i++){
    arrays[i][(arrays.length - 1) - i] = '-'
}
console.log(arrays);

please help...

I tried debugging, taking it out of function, changing the name, but it didn't work.

Konrad
  • 21,590
  • 4
  • 28
  • 64
bakhacode
  • 3
  • 1
  • `.push(array)` - `array` stores a reference to an array. You're pushing that reference into `addedEmptyArrays` - but you need an actual copy of the array the reference points to -> `.push(array.slice())` or `.push([...array])` or ... – Andreas Jan 12 '23 at 18:12

2 Answers2

0

if you look at this line

addedEmptyArrays.push(array)

you are pushing same array reference again and again so any modification you do here

arrays[i][(arrays.length - 1) - i] = '-'

it is reflected in all the instances ( because they are same instances )

fix is to create a new instance every time you are pushing to the array, just replace the line pointed above with

addedEmptyArrays.push(createEmptyArray(centralValue))

this should give expected result and you can refactor your code to create new instances while pushing

ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

Arrays are passed by reference

[...array] will fix your problem

let input = 6;

const diameter = input; //6
const centralValue = Math.ceil(diameter / 2); //3

const reverseArray = (array) => array.slice(0).reverse() //어레이 뒤집어줌


let createEmptyArray = (number) => Array(number).fill(' ') //빈 배열 생성

let emptyArray = createEmptyArray(centralValue);

let addEmptyArrays = function(array){
    let addedEmptyArrays = [];
    for(i = 1; i <= centralValue; i++){
        addedEmptyArrays.push([...array])
    }
    return addedEmptyArrays;
}
let addedEmptyArrays = addEmptyArrays(emptyArray);


let arrays = addedEmptyArrays
//[ [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ], [ ' ', ' ', ' ' ] ]

console.log(arrays);

for(i = 0; i < arrays.length; i++){
    arrays[i][(arrays.length - 1) - i] = '-'
}
console.log(arrays);
Konrad
  • 21,590
  • 4
  • 28
  • 64