Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:47:33+00:00 2026-06-09T05:47:33+00:00

Im building a small random sequence comparitor in order to learn js objects, at

  • 0

Im building a small random sequence comparitor in order to learn js objects, at the moment I’m having a problem where if I increment the limit variable within my completeMessage() this gets automatically run within the the for loop of my compareSequences() resulting in an infinite loop, could anyone advise me where I may being going wrong?

2 methods in question:

compareSequences: function() {                
    var instance = this;
    var i = 0;

    for( i; i <= limit; i++ ) {
        console.log('Limit inside loop:', limit);

        if( stacks.randomSequence[i] != stacks.userSequence[i] ) {
            instance.errorMessage();
            return;
        }                 

        if( i === limit ) {
            instance.completeMessage();
        }

        console.log('Limit now', limit);
    }               
},

completeMessage: function() {
    var instance = this;
    alert('Well done you where right!');
    limit = 5; //currently set as test, changing to ++ results in infinite loop
    instance.selectors.startButton.removeAttr('disabled');
    overallScore++;
    instance.selectors.scoreCounter.html(overallScore);
},

Link to my fiddle: http://jsfiddle.net/FNd79/22/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:47:35+00:00Added an answer on June 9, 2026 at 5:47 am

    Your limit variable is by default defined on the global scope (as a property of the object window) changing it’s value in one function changes it everywhere.

    Be sure to define it as a local of your class using var.

    Your issue is that you modify your upper bound during the loop, in a way that it increases of one every time you are going to exit the loop.

    You need the global value at the time you init the loop, you can just copy it to a local variable at that time:

    compareSequences: function() {                
      var instance = this;
      var i = 0;
      var max = limit;
      for( i; i <= max; i++ ) {
          ...
      }
      ...
    }
    

    You could alternatly use the keyword break to exit the loop for sure.

    if( i === limit ) {
      instance.completeMessage();
      break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am building a small simple JS framework for University. I am having
Im building a small webshop and have run into a problem. The company which
I´m building a small application with Jquery and PHP. Jquery (index.html) adds Form Fields
I'm building a small filter utility for users to quickly filter a list of
I'm building a small twitter style microblogging service where users can follow other users
I am building a small applet in java. It works fine (really!) when I
I'm building this small java applet in which I need a JPanel which will
I am building a small rails app and I need a way to generate
I am building a small facebook app. Trying to search for values in an
I'm building a small web search engine for searching about 1 million web pages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.