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 8305713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:10:42+00:00 2026-06-08T18:10:42+00:00

I have a problem. I need a Javascript function which increase (increment) variable value

  • 0

I have a problem.

I need a Javascript function which increase (increment) variable value by 4, and when the variable value is 20, then set value of variable to 0 and again increment it by 4 and so on…

I think that I need for loop and if condition, but I don’t know how to implement this…

Example

the result must be:

x = 0; then x = 4, x = 8, x = 12, x = 16, x = 20, x = 0, x= 4 ….

Thank you

  • 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-08T18:10:44+00:00Added an answer on June 8, 2026 at 6:10 pm

    You can do this with a nested pair of loops:

    while (true) {
        for (var x = 0; x <= 20; x += 4) {
            // use x
        }
    }
    

    This will be more efficient than using the mod (%) operator.

    EDIT

    From your comments, it sounds like you want to generate the sequence incrementally, rather than in a loop. Here’s a function that will return a function that will generate the next element of your sequence each time you call it:

    function makeSequence() {
        var x = 20; // so it wraps to 0 first time
        return function() {
            if (x == 20) { x = 0 }
            else { x += 4 }
            return x;
        }
    }
    

    You could then use it like this (among many ways):

    var sequence = makeSequence();
    
    // and each time you needed the next element of the sequence:
    
    var x = sequence();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that contains a javascript function that i need to init/call
I have a javascript function that executes at page load, which is like this:
I have a problem that I need to sort out in javascript I believe,
I have a problem to test my non activity-class which need the context of
I have an onclick function which performs several tasks. In another JavaScript function I
I have a javascript function that, in most cases, needs to do something with
I'm using javascript and jQuery. I have a problem that my web app has
I have a javascript function I'm writing which is being used to include an
There is a JavaScript function, of which I have zero control of the code,
I have strange problem for which I can't think of a solution. I have

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.