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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:57:16+00:00 2026-05-12T08:57:16+00:00

Given this code: var minX = minY = maxX = maxY = 0; for(var

  • 0

Given this code:

    var minX = minY = maxX = maxY = 0;

    for(var i=0; i<objArray.length; i++){
        if(objArray[i].x < minX){
            minX = objArray[i].x;
        }else if(objArray [i].x > maxX){
            maxX = objArray[i].x;
        }
        if(objArray[i].y < minY){
            minY = objArray[i].y;
        }else if(objArray [i].y > maxY){
            maxY = objArray[i].y;
        }
    }

It works, but I don’t think it is very elegant.
It is simple logic, but it uses 10 lines of code. Can it be improved?

  • 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-12T08:57:17+00:00Added an answer on May 12, 2026 at 8:57 am

    You could use Math.min and Math.max:

    var minX = minY = Number.POSITIVE_INFINITY,
        maxX = maxY = Number.NEGATIVE_INFINITY;
    
    for(var i=0; i<objArray.length; i++){
      minX = Math.min(objArray[i].x, minX);
      minY = Math.min(objArray[i].y, minY);
      maxX = Math.max(objArray[i].x, maxX);
      maxY = Math.max(objArray[i].y, maxY);
    }
    

    For loop speed optimization you could store the length to only calculate it once:

    for(var i=0, len = objArray.length; i<len; i++){
      //...
    }
    

    Check this article for more information about loop optimization.

    Another approach, just for “functional fun”, since I wouldn’t recommend it for performance, could be to separate the x and y values to two arrays using Array.map, and then call the min and max functions with apply:

    var allX = objArray.map(function (o) { return o.x; });
    var allY = objArray.map(function (o) { return o.y; });
    
    minX = Math.min.apply(Math, allX);
    minY = Math.min.apply(Math, allY);
    maxX = Math.max.apply(Math, allX);
    maxY = Math.max.apply(Math, allY);
    

    How this works?

    The apply function is used to call another function, with a given context and arguments, provided as an array. The min and max functions can take an arbitrary number of input arguments: Math.max(val1, val2, …, valN)

    So if we call:

    Math.min.apply(Math, [1,2,3,4]);
    

    The apply function will execute:

    Math.min(1,2,3,4);
    

    Note that the first parameter, the context, is not important for these functions since they are static, they will work regardless of what is passed as the context.

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

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $("body").find("span").css("border", "2px red solid"); vs $("body").find("span").end().css("border", "2px red solid"); Execute… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer Summary: For about 1 million active users and 150 million… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer Turned out that what I needed to do was to… May 12, 2026 at 9:50 pm

Related Questions

Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this code: using (var conn = new SqlConnection(...)) { conn.Open(); using (var cmd
Given this LINQ to SQL: using (var db = Database.Context) { var root =
In one part of the application I'm working on, there is a form control

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.