0

i need one more thing

i have

function show_image()
{
  document.write("<img src="+this.link+" ,alt="+this.alt+" ,width=600, height=400>");
}

function image(a,b,c)
{
  this.link=a;
  this.alt=b;
  this.thumb=c;
  this.zobraz=show_image;
}

image1=new image("img/img1.jpg","info image 1","thumb/img1");
image2=new image("img/img2.jpg","info image 2","thumb/img2");

and now, i am trying to do something like this:

function slideshow()
{
var i=1;
      while(i<3)
{
      document.write(image[i].zobraz());
i++
}
}

i don't know why, but this [i] thing is not working. Where is my mistake? thanks

4 Answers 4

1

You should use an array to be able to subscript using []:

// create array to hold images
var images = [];

// load images and store them in the array
images.push(new image(...));
images.push(new image(...));

// .. now subscripting using [] works fine now
var i=0;
while(i<3)
{
   document.write(images[i].zobraz());
   i++
}

NOTE Array indices are zero-based, i.e. the first entry is at index 0!

Sign up to request clarification or add additional context in comments.

4 Comments

so, i can't continue this way? Basically, my constructor is useless now, right?
It isn't useless, it is unrelated to the problem - the problem is simply that you didn't create an Array, so accessing using the [0] notation doesn't work.
Small improvement: while( i < images.length ). In case a fourth and fifth image appear...
this is great. Just one more thing : image.push(new image("img/img2.jpg","info o image 2","thumb/img2")); this is not working for me, but it is not such a big deal
0

You haven't actually created an array.

Try this:

var image = [];
image[0]=new image("img/img1.jpg","info image 1","thumb/img1");
image[1]=new image("img/img2.jpg","info image 2","thumb/img2");

and adjust your loop.

Comments

0

You are attempting to reference image1 and image2 as though they are array elements.

Instead of

image1=new image("img/img1.jpg","info image 1","thumb/img1");
image2=new image("img/img2.jpg","info image 2","thumb/img2");

Try this:

var image = [];
image[1] = new image("img/img1.jpg","info image 1","thumb/img1");
image[2] = new image("img/img2.jpg","info image 2","thumb/img2");

Now your function slideshow() should work as you've written it.

Comments

0

In addition to the array-business stated in the other answers, you're still doing something awkward there:

document.write( image[i].zobraz() );

If you would expand the zobraz call you will see that it doesn't actually return anything. It would be best to either make zobraz return the "img" string, or to code simply image[i].zobraz();

2 Comments

i got some weird error(it has displayed image, but alsostated undefined), but i couldn't find the problem. And this is the problem :) thanks
@sevdah: I saved you asking the next question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.