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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:30:30+00:00 2026-06-06T08:30:30+00:00

To find the next odd number for an input the following code is being

  • 0

To find the next odd number for an input the following code is being used:

a=5.4; // Input
b=Math.ceil(a); // Required to turn input to whole number 
b=b+(((b % 2)-1)*-1); // Gives 7

The ceil rounding function is required.

Is this safe and is there a more compact way to do this?

EDIT: When the input is already an odd whole number then nothing happens. For example 5.0 will return 5

  • 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-06T08:30:32+00:00Added an answer on June 6, 2026 at 8:30 am

    At the question author’s request:

    The most compact way to achieve it is

    b = Math.ceil(a) | 1;
    

    First use ceil() to obtain the smallest integer not smaller than a, then obtain the smallest odd integer not smaller than ceil(a) by doing a bitwise or with 1 to ensure the last bit is set without changing anything else.

    To obtain the smallest odd integer strictly larger than a, use

    b = Math.floor(a+1) | 1;
    

    Caveats:

    Bit-operators operate on signed 32-bit integers in Javascript, so the value of a must be smaller than or equal to 2^31-1, resp. strictly smaller for the second. Also, a must be larger than -2^31-1.

    If the representation of signed integers is not two’s complement, but ones’ complement or sign-and-magnitude (I don’t know whether Javascript allows that, Java doesn’t, but it’s a possibility in C), the value of a must be larger than -1 — the result of Math.ceil(a) resp. Math.floor(a+1) must be nonnegative.

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

Sidebar

Related Questions

I am writing a simple program, which is trying to find next palindrome number
How does this code work to Find the next highest power of 2 for
How to find next business day with powershell ?
I have a small problem where I want to find the next active item
looper = (0..3).cycle 20.times { puts looper.next } can I somehow find the next
Ideally, im trying to click a div, find the next class within a parent.
I find the article titled How Basecamp Next got to be so damn fast
I remapped [[ and ]] to find the previous and next pattern in the
Hope find a guru's help to figure out the next problem. I have two
Ive been making a find, find next function for my richtextbox, so 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.