0

I have this simple code

let pathFinder = (entranceX, entranceY, grid) => {
            let distanceFromTop = entranceX;
            let distanceFromLeft = entranceY;

            let location = {
                distanceFromTop: distanceFromTop,
                distanceFromLeft: distanceFromLeft,
                path: [],
                status: 'Start'
            }


            console.log('start', location)

            let queue = [];
            queue.push(location);
            console.log('queue', queue)
}

And I made sure that "location" is not empty, it shows just fine in the console as:

"start {distanceFromTop: 1, distanceFromLeft: 0, path: Array(0), status: "Start"}"

but when I want to add it to the queue I end up with something that doesnt look like an empty array, but the length is 0

"queue [{…}] length :0"

Am i missing something obvious? Tried to add it via queue[location] but it also didnt work

6
  • tested it and seemed fine. sure you're executing it i mean you are just setting the queue varible within that scope and you're not returning it Commented May 26, 2018 at 14:09
  • How are you calling this function? Commented May 26, 2018 at 14:11
  • can you give us abit more informarion on how you're exicuting and what where you're trying to access queue. Commented May 26, 2018 at 14:12
  • I execute the function by button click. Then queue is used to store more locations in pathdinging algorythm. However, the queue I pass to next functions is basically empty even though it shouldnt Commented May 26, 2018 at 14:30
  • @MazMat, can you please change all of let keywords to var and try. Your code is not working when I tested it on rextester.com/l/nodejs_online_compiler. When I changed all let to var it worked fine. I got start { distanceFromTop: 23, distanceFromLeft: 56, path: [], status: 'Start' } queue [ { distanceFromTop: 23, distanceFromLeft: 56, path: [], status: 'Start' } ] as result. Commented May 26, 2018 at 14:53

2 Answers 2

1

You should check up on the difference between let and var

var pathFinder = (entranceX, entranceY, grid) => {
        var distanceFromTop = entranceX;
        var distanceFromLeft = entranceY;

        let location = {
            distanceFromTop: distanceFromTop,
            distanceFromLeft: distanceFromLeft,
            path: [],
            status: 'Start'
        }


        console.log('start', location)

        var queue = [];
        queue.push(location);
        console.log('queue', queue)
}

Let can only be used in the scope block in which it’s declared where as var is throughout the function it is declared or globally. Changing the first three (the function and the two distance as well as the queue) variables to var should fix this for you. If not changing them all definitely would.

Sign up to request clarification or add additional context in comments.

10 Comments

Changed everything in my code to var. No changes to how it behaves. Does it matter that I use react with this? Probably shouldnt, but im completely lost now
It may be when your passing variables. When I test it on a native JavaScript compiler and run the function manually. It works as expected. Maybe post more of your code and we may be able to help. There may be an error elsewhere in your code. - also to reference the array I used queue[0] not queue[location]
I will prepare the codepen instance for that, because I have 2 files.
codesandbox.io/s/zkz3l2oxy3 Relevant code is in maze.js in findTest() function. There might be a bit of a mess since I was trying a lot of different things out before I tried this pathfinding algorythm. The grid doesnt seem to be rendering correctly in the sandbox, but other than that everything is like on my machine. Exactly same conosle output too
@MazMat findTest is never actually ran in the code you provided. Could this be the reason why??
|
1

Looks like the push is working but the array is not being displayed while logging the whole object

queue.push(location);
console.log("queue", queue[0]);

queue {distanceFromTop: 1, distanceFromLeft: 1, path: Array(0), status: "Start"}

And if you change the console as,

console.log("queue", Array.from(queue));

You can see the contents of the array too.

2 Comments

Huh, Youre right, but why would that be this way? I never had such problems with displaying huge arrays of objects.
console.log("queue", Array.from(queue)); to see all the contents of the array

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.