13293

Incrementing an integer in javascript results in NaN

Question:

I've tried to debug this but I just don't understand why the variable disp returns as NaN whenever I increment it by one. If anyone could shed some light on why this is happening I'd appreciate it tenfold.

var votePages = new Array(); votePages[0] = "http://minecraftservers.org/vote/100924"; votePages[1] = "http://www.planetminecraft.com/server/revolutionarycraft---ssdfactionsecosurvivalgrief/vote/"; votePages[2] = "http://minecraftservers.net/server/66267/vote/"; votePages[3] = "http://mineservers.com/server/11885/vote"; votePages[4] = "https://minestatus.net/92187-revolutionary-craft/vote"; var disp = 0; $("#dispVotePage").attr("src", votePages[disp]); $(document).on('click', '#next', function(){ $("#dispVotePage").attr("src", ''); var disp = disp++; alert(disp); $("#dispVotePage").attr("src", votePages[disp]); if (disp !== 0) { $("#previous").css('display', 'block'); } }); $(document).on('click', '#previous', function(){ $("#dispVotePage").attr("src", ''); var disp = disp--; $("#dispVotePage").attr("src", votePages[disp]); if (disp === 0) { $("#previous").css('display', 'none'); } });

You'll see that I have alert(disp) in there, its for debugging purposes.

Answer1:

Re-declaration of disp. You are declaring it twice:

var disp = 0; //Initial Declaration (Global) ... <snip> ... $(document).on('click', '#next', function(){ $("#dispVotePage").attr("src", ''); var disp = disp++; //Re-declaration. Declares `disp` as a new local variable. alert(disp); $("#dispVotePage").attr("src", votePages[disp]); if (disp !== 0) { $("#previous").css('display', 'block'); } });

Solution: To use the global disp, do not declare it again. Just use disp++

Answer2:

It looks like you are declaring a new disp variable in your functions, even though you have already done so after your array at the top. Get rid of the var disp and just increment or decrement it.

Answer3:

looks like it is probably due to var disp = disp++ try disp=disp+1 (without a var) instead. And similarly for decrements. I think it should fix this!

Recommend

  • Python Praw skipping sticky in subreddits
  • how does zookeeper do when the master down
  • Rails: Include all of model A and only a subset of related model B
  • Prevent users from voting multiple times in Rails
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • Neo4j one-to-many fetch data
  • Emacs lua-mode issue: (void-function interactively-called-p)
  • sudo easy_install for SimpleITK not that easy
  • Invert string in Rust
  • How to create a new Bundle object?
  • Separate ID and Class for JS and CSS
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • Any nice way to generate a timeline view of commits from subversion?
  • Sybase Error Implicit Conversion from datatype 'VARCHAR' to 'INT' not allowed
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • How to retrieve information from antrun back to maven?
  • Do query loads all the data in memory
  • aapt.exe'' finished with non-zero exit value 1
  • Salesforce Different WSDL files and when to use
  • AndEngine Applying Transparancy to AndEngine View
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Jetty 9 HashLoginService
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • Why is an OPTIONS request sent to the server?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Launch Runnable Jar from Web Start
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Get object from AWS S3 as a stream
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j