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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:34:37+00:00 2026-05-25T06:34:37+00:00

What I would like to do is replace the two if statements with a

  • 0

What I would like to do is replace the two if statements with a single mathematical formula. I can’t for the life of me figure out how, as mathematics was never my strong point. Any advice would be greatly appreciated.

<script>

var x, y, z;
x = 200;
y = 100;
i = 0;

 while(z != y) {

    i++;

    if (x < y) z = x + i;
    if (x > y) z = x - i;

    document.write(z + "<br>");

}


</script>

Edit: the real code looks like this. It’s not too pretty, was hoping I could shrink it down to two lines.

if (prevposX < newposX) posX = prevposX + animStep_;
if (prevposX > newposX) posX = prevposX - animStep_;
if (prevposY < newposY) posY = prevposY + animStep_;
if (prevposY > newposY) posY = prevposY - animStep_;

Edit:

It has been a while, but I believe the Modulus (%) operator would have helped me. I’ve now moved on to a library to do graphics for me so I didn’t need it in the end.

  • 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-25T06:34:37+00:00Added an answer on May 25, 2026 at 6:34 am

    You can make use of a ternary operator

    z = x + ((x<y) ? i : -i)
    

    This assumes that when x >= y then you want to -i. If you want to stick exactly to your original you need a second ternary operator, which gets a bit messy:

    z = x + ((x<y) ? i : ((x>y) ? -i : 0))
    

    Frankly, it might be clearer to just keep it on 2 lines with 2 if statements like you already have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I search and replace in way described below. I would like have
I would like to replace only the group in parenthesis in this expression :
I would like to replace the default malloc at link time to use a
I would like to replace the entire header area in an NSIS installer with
I would like to replace drop down list (that has on change event that
I would like to replace the first character ' x ' with the number
in my script I check some files and would like to replace a part
In a text, I would like to replace all occurrences of $word by [$word]($word)
In Eclipse 3.3.2, I would like to replace a character (say ',') by a
I'm using jQuery with the validators plugin. I would like to replace the required

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.