codewars / codewars.com

Issue tracker for Codewars
https://www.codewars.com
BSD 2-Clause "Simplified" License
2.1k stars 220 forks source link

global not reseting from test to test #3017

Closed luis-orantes closed 1 year ago

luis-orantes commented 1 year ago

go to this kata: https://www.codewars.com/kata/53897d3187c26d42ac00040d/train/javascript

and use this code:

` let rounds=0; let path=[];

// a and b are of type Node // return whether there is a route from a to b function recursive(a, b, next) { rounds++;

if(!next) { next=a; } else if(next===b) { // we arrived to destination path.pop(); return true }

for(let node of next.edges) { if(!path.includes(node)) { path.push(node); if(recursive(a, b, node)) return true; } }

path.pop(); return false; }

function getRoute(a, b) { // console.log('a: '); // console.log(a) // console.log('b: '); // console.log(b)

// path=[];

const result=recursive(a, b); return result; } `

the test will fail. But now uncomment // path=[]; in getRoute. Now the test will succeed

This behavior is not presented when I run the code locally.

This should happen since I am reseting path globally in the following code in the first lines: let rounds=0; let path=[];

The problem, is that for some reason the global variable is keeping its value from one test to another in although I am resting the variable in the global declaration.

hobovsky commented 1 year ago

This is expected and its exactly how global variables work. If you use global variables in your local IDE, they most probably work in exactly same way as in CW tests. If it works differently in your IDE than on CW, then most probably your setup is different than on CW.

Tests are usually just a regular program, and global variables in your solution work just like global variables in any other program.

This should happen since I am reseting path globally in the following code in the first lines: let rounds=0; let path=[];

This is not how "resetting global variables" is done.

Please use code formatting when posting snippets of code.