question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global variable conflicts with algorithm.

See original GitHub issue

Challenge Factorialize a Number has an issue. User Agent is: Mozilla/5.0 (Windows NT 5.1; rv:47.0) Gecko/20100101 Firefox/47.0. Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

var count=1;
function factorialize(num) {
  for(var i=1; i<=num;i++){

    count= count * i;

  }
  return count;

}

factorialize(0);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
Locheedcommented, Jul 28, 2016

Just spend some time figuring out why my test fails and it was this problem with a global variable.

Just a thought, how about adding a note to descriptions, that “Using global variables in this test will cause the test to fail.” I think it would help in this kinda situation, but doesn’t give too many tips for an exercise.

3reactions
raisedadeadcommented, Jun 17, 2016

@krishnakumar360 : Thanks for reporting this.

Your code is correct but using the var count=1 in the global scope is causing the test cases to fail. Moving it to the function’s scope should let you pass the challenge.

function factorialize(num) {
  var count = 1;
  for(var i=1; i<=num; i++) {
   count = count*i;
...

The reason being that we run multiple test cases in on the code, using a global var like this persists the last value and hence the test cases fail.

Nevertheless this is valid code and we should be addressing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alternatives To Global Variables - Anupam Chugh - Medium
Disadvantages of Global Variables · Violates Encapsulation · Unit Testing Issues · Dealing With Concurrency · Naming Conflicts.
Read more >
Conflicts in Local / Global Variables can be Unproductive
Conflicts that are created for local/global variables is to be taken seriously because it decreases code productivity which causes loss of time and...
Read more >
conflict between local variable and global variable - YouTube
Hi friends,In this video i covered local variable and global variable available in c language. i started with declaring local variable in ...
Read more >
Why to avoid global variables in JavaScript ? - GeeksforGeeks
This indicates that global variables can be accessed from anywhere in the particular script and are not restricted to functions or blocks.
Read more >
global variables conflict between TCL sourced files
It's not guaranteed that these won't conflict — they're not strongly separated — but they might work. It's definitely an easy first thing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found