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

  • SEARCH
  • Home
  • 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 7496105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:28:59+00:00 2026-05-29T18:28:59+00:00

Basically, I want a function I can call which lets me iterate in a

  • 0

Basically, I want a function I can call which lets me iterate in a bouncing-loop between a range of numbers by a specified increment. I’ve tried way complex solutions with many conditionals but it seems to me this should really be a simple math one-liner no?

I’m having a bit of trouble formulating my question so here is some pseudo coffescript to better explain my goals.

# Pseudo Coffeescript class

Class OscillatingIterator

    constructor: ( low, high, increment )->
        this.low = low
        this.high = high
        this.i = increment

    iter: ->
      ### can haz magical math code plz? ###

# Usage

oi = new OscillatingIterator( 1, 5 , 1 )

# outputs

oi.iter() #=> 1
oi.iter() #=> 2
oi.iter() #=> 3
oi.iter() #=> 4
oi.iter() #=> 5
oi.iter() #=> 4
oi.iter() #=> 3
oi.iter() #=> 2
oi.iter() #=> 1
oi.iter() #=> 2
oi.iter() #=> 3
oi.iter() #=> 4
oi.iter() #=> 5
oi.iter() #=> 4
oi.iter() #=> ...
  • 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-05-29T18:29:00+00:00Added an answer on May 29, 2026 at 6:29 pm

    I’ve slightly modified the version written by ori. I think this is a kind of behavior that oscillator should have. Changing the sign of increment to bounce from the bound is only a part of what should be done, because it is good for step = 1 only. Example – oscillator between -2 and 3 with step 2. The code above will give -2, 0, 2, 0, -2, 0, 2 and so on.

    I think it should be -2, 0, 2, 2 (1 step from 2 to 3 and 1 step to return), 0, -2, 0 and so on.. The same for (-4,4,3). It should be -4, -1, 2, 3, 0, -3, -1 and so on (the original code will give -4, -1, 2, -1, -4, ..). My suggestion is (I also added one more checkup for the values of the parameters)

    function oscillator(low, high, inc) {
    
        // basic test for illegal parameters
        if (low > high || inc < 0 ||  2 * (high - low) < inc) 
            return function() { return NaN; };
    
        var curr = low;
        return function() {
            var ret = curr;
            curr += inc;
    
            if (curr > high || curr < low) 
            {   
                curr = inc>0 ? 2 * high - curr: 2 * low - curr;  
                inc = -inc;
            };
    
            return ret;
        }; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a jQuery .each() call, which uses an anonymous function, from inside a
i have a function that i can call to apply a rounded corner fix
I have a js function that is named getID which is basically return document.getElementById(id)
I have a simple function I want to call in two separate program executions.
So basically I want to add a javascript function to a button's Click event
How do I call Zedgraph's function masterPane.SetLayout() from Web C# Application? Basically i'm stuck
I basically want to use link_to to link to the index method of a
I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
I basically want to be able to deploy multiple versions of the same EAR

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.