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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:52:54+00:00 2026-06-04T11:52:54+00:00

How can I craft a one-liner return line that also has conditionals? For example,

  • 0

How can I craft a one-liner return line that also has conditionals? For example, if I want to make a ‘return the median’ one:

//assuming sorted input array
return ((inputArray.length % 2) && (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2) || inputArray[int(inputArray.length/2)+1];

Is there anyway to make this work?

  • 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-04T11:52:55+00:00Added an answer on June 4, 2026 at 11:52 am

    You should use the ternary operator:

    return (inputArray.length % 2 != 0) ? (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2 : inputArray[int(inputArray.length/2)+1];
    

    Which is equivalent to:

    if (inputArray.length % 2 != 0) {
        return (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2;
    } else {
        return inputArray[int(inputArray.length/2)+1];
    }
    

    If you want to use only && and ||, you can use the following (which is not really a good programming style):

    ((inputArray.length % 2 != 0) || return inputArray[int(inputArray.length/2)+1]) && return (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]);
    

    Which is equivalent to:

    (condition || return value2) && return value1;
    

    So, thanks to the short-circuit evaluation of boolean operators:

    • if condition is true, return value2 is not evaluated and return value1 will be executed.
    • if condition is false, return value2 will be evaluated.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a reduced example of what I want to do. The line commented
It doesn't seem possible to craft a connection string so that you can open
can I make my own headers in HTTP request ? e.g. This is normal
Can I figure out if a function has already been assigned to an event?
My company is engaging with a multivariate testing vendor (I can't disclose which one
I'm looking for a C++ library that can parse 32-bit and 64-bit Mach-O binary
I have a script that connects to a mine craft server, receives packets, and
I have an issue that on one computer applications developed using the Windows Presentation
Suppose I have a search for government registered charities, how can I craft my
How do you craft a switch statement in as3 to make the case apply

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.