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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:44:59+00:00 2026-06-10T21:44:59+00:00

I have this small source of code for (i = 0; i < elements.length;

  • 0

I have this small source of code

for (i = 0; i < elements.length; i++) {
     console.log(i);
     (function (i) {
         if (elements[i]["id"] == id) {
             index = i;
             console.log(i);
         }
     })(i);
}

console.log(index);

I want to find an array’s element index where sub element “id” = id; but it returns sometimes 0 simetimes 1. This code is inside a function that is inside a class. and I call the same function twice but with different parameters outside of the class, so first it must show 0 and than 1, but sometimes it shows 1 and than 0. so can you help me? I tried this method too but it doesn’t work.

var i = 0;
while (i < elements.length) {
    console.log(elements[i]["id"]);
    if (elements[i]["id"] == id) {
        index = i;
        break;
    } else if (elements[i]["id"] != id) {
        i++;
    }
}

console.log(index);

Here is my test page, If you open it with Chrome (sometimes it happens but in Firefox it happens mostly) you will see that sometimes small image appears but sometimes it disapears when you press the button. You can also see the console output in “inspect elements -> console”

EDIT

I changed my code for better debug like this (as Lando suggested) :

for (i in elements){
    console.log("comparing elements['"+ i +"']['id']: "+ elements[i]["id"] +" with id:' "+ id + "'");
        if(elements[i]["id"] == id){
            index = i;
        }
    }

console.log(index);

and got console ouput:

comparing elements['0']['id']: id2 with id:' id1' Slideshow.js:140
comparing elements['1']['id']: id1 with id:' id1' Slideshow.js:140
1 Slideshow.js:147
comparing elements['0']['id']: id2 with id:' id2' Slideshow.js:140
comparing elements['1']['id']: id1 with id:' id2' Slideshow.js:140
0 

When it should be:

comparing elements['0']['id']: id1 with id:' id1' Slideshow.js:140
comparing elements['1']['id']: id2 with id:' id1' Slideshow.js:140
0 Slideshow.js:147
comparing elements['0']['id']: id1 with id:' id2' Slideshow.js:140
comparing elements['1']['id']: id2 with id:' id2' Slideshow.js:140
1 

EDIT 2

After seeing the output it turns out that objects in array “elements” change places, elements[0]["id"] becames "id2" where it should be "id1" I can’t still difinetly say why is this happening or what to do to fix it. so please if you have any ideas share it.

Here is my JSfiddle link

  • 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-10T21:45:01+00:00Added an answer on June 10, 2026 at 9:45 pm

    I found what was the problem. It seemed that sometimes small image was loaded the first so large image was appearing at the top. It is all about image loading. Thank you anyway for your answers, everyone helped me to debug the problem.

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

Sidebar

Related Questions

I have written small C++ console application and this is source code : #include<stdio.h>
I have this small code File source; if ( !source.exists() ) { source =
I have this small code library that I'm considering releasing into Open Source. I
Say I have this small function in a source file static void foo() {}
I have this small piece of code that basically takes a list and runs
Work on this small test application to learn threading/locking. I have the following code,
I have a small set of source code files for C. I'm trying to
Suppose you have some source code that comes from the unix world. This source
I have this source code at the moment, but the issue I am having
I have this small class looking like this: private static int field1 = -

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.