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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:05:44+00:00 2026-06-03T04:05:44+00:00

i am trying to order some images in columns based on their height and

  • 0

i am trying to order some images in columns based on their height and i have the following code.
the problem is that even though imgHeights.length works fine, imgHeights[i] (in the redrawThumbs function) doesn’t return a value.
what am i doing wrong? thanks =)

function redrawThumbs(imgHeights){
  // some irrelevant code here

  // initialise an array that will hold all the column's heights
  var colHeights = new Array(cols);
  for(a=0; a <= colHeights.length - 1; ++a){
    colHeights[a] = 0;
  }

  // take each image's height and add it to the shortest column
  for(i=0; i <= imgHeights.length - 1; ++i){
    var shortestCol = 0;

    for(c=0; c <= colHeights.length - 2; ++c){
      if(colHeights[c+1] < colHeights[c]){
        shortestCol = colHeights[c+1];
      }
    }

    alert("imgHeights[" + i + "] " + imgHeights[i]);

    colHeights[shortestCol] += imgHeights[i];
  }
}

// make an array of image heights
var imgHeights = new Array(totalThumbs);    
for(i=1; i <= totalThumbs; ++i){
  var img = new Image();
  img.onload = function(){
  imgHeights[i-1] = this.height;
}
img.src = i + ".jpg";

// call function that orders the images
redrawThumbs(imgHeights);
  • 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-03T04:05:46+00:00Added an answer on June 3, 2026 at 4:05 am

    One of the issues is that the i variable is captured by reference, not by value so, for all the callback of img.onload is getting the same value of i. Consider changing the onload assignment to,

    var loadedCount = 0;
    img.onload = (function (i) { 
        return function () { 
            imgHeights[i - 1] = this.height;
            if (++loadedCount == totalThumbs) allLoaded();
        };
    })(i);
    
    function allLoaded() {
        // called after all the images have been loaded.
    }
    

    this captures a copy of the value of i at the point of auto-execute function execution and calls allLoaded() after all the images have been loaded.

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

Sidebar

Related Questions

I'm trying to save a file in which I have some credentials. In order
I have a strange problem, I am trying to order the output of a
I'm trying to execute a few python scripts in order to manipulate some images
I have create my code in order to write images in a remote sql
I'm trying to use XSL-stylesheets in order to transform some generated XML-data to HTML-output.
I am trying to use SDO2SHP.exe in order to export some spatial data from
I'm trying to unmarshall some dozer mapping files in order to provide a mapping
I'm trying to order models in a Django app using a field that has
In order to condense my code I am trying to make one of my
I am trying to create a drawable in code and change the color based

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.