Jump to content

Javascript Assistance Please, What Am I Doing Wrong


Web Weenie

Recommended Posts

I thought I knew how this works but something is wrong. Can some give this a lookover and see where I've gone off the tracks...the js:

var myindex=4;var bon=2000;var myval=new Array(50000,5000,4000,3000,2000,1000)var myimg=new Array("box1.jpg", "box2.jpg", "box3.jpg", "box4.jpg", "box5.jpg", "box6.jpg")function changeimage(v){if (v==1) {myindex=myindex-1;}else if (!v==1){myindex=myindex+1;}bon=myval[myindex]; // set bon to an element of myval arraydocument.getElementById('mypic').src=myimg[myindex]; set image name as an element of myimg array}The HTML:<img src="Dn.jpg" onclick=" changeimage(1);" /><img id="mypic" src="box5.jpg"" /><img src="Up.jpg" onclick=" changeimage(0);" />

Also how do I add an additional condition so myindex does not go below 0 (zero) and above 5.

Link to comment
Share on other sites

have you tried else if (v==0)?to check myindex, something like this:

if(myindex < 0 || myindex > 5){//code for what you want to happen if either of those conditions is met};

Link to comment
Share on other sites

Ok, I solved the conditional statement and it is working...function changeimage(v){if ( v==1 && myindex > 0 ) {myindex=myindex-1;}if ( v==0 && myindex < 5) {putting an alert here shows myindex in being incremented and decremented and does no fall below 0 or raise above 5}bon=myval[myindex];putting an alert here does not display so the syntax is wrong can you explain?document.getElementById('mypic').src=myimg[myindex];same for here alert does not show}What is wrong with the last two lines? Both involving arrays.

Link to comment
Share on other sites

are you saying the alert doesn't show, or that nothing shows in the alert?perhaps it's your syntax for calling the alert (which you didn't include).

Link to comment
Share on other sites

Just to clarify:!v==1 is not the syntax you wanted. Changing the code to v==0 fixed the problem, but maybe you don't know what was wrong with the first one. It should have been this:v!=1

Link to comment
Share on other sites

It's the same alert message being placed in different places so it's not the syntax of the alert.I showed the alert in the wrong place. the first red line show it inside the if/then and it should be just outside itso are you saying you don't see anything wrong with these tow lines?bon=myval[myindex];document.getElementById('mypic').src=myimg[myindex];I isolated this down to those and I don't know what is wrong or why...

Link to comment
Share on other sites

can you repost (in a new post) what your code is now? No psuedo code unless it is in a comment. Alerts and all that included.

Link to comment
Share on other sites

here is the code with a try/catch error checking

var myindex=4var myval=new Array(50000, 5000, 4000, 3000, 2000, 1000)var myimg=new Array("images/scroll0.jpg","images/scroll1.jpg","images/scroll2.jpg","images/scroll3.jpg","images/scroll4.jpg","images/scroll5.jpg");function changeimage(v){if ( v==1 && myindex > 0 ) {myindex=myindex-1;} if ( v==0 && myindex < 5 ) {myindex=myindex+1;}try {bon=myval[myindex];//document.getElementById('mysrc').src=myimg[myindex];} catch( myError ) {  //if an error occurs, this code will be run  //two properties will (by default) be available on the  //object passed to the statement  alert( myError.name + ': ' + myError.message );} finally {  //optional - this code will always be run before the  //control structure ends, even if you rethrow the error  //in the catch}}

now here is what is reported... TypeError: Result of expression 'myval' [undefined] is not an object.

Link to comment
Share on other sites

there was no psudo-code by the way, I was just posting where the alert succeeded and where it failed.I have also replaced the var myindex with the number 1 and get the same error.when using an alert to display the var myindex it reports the correct value.there is something wrong with how I'm assigning the array or how I'm using it to assign a value from an array. I'm thinking it how I'm defining the array

Link to comment
Share on other sites

No that didn't work either... But I did find the problem.I moved the array definitions inside the function and it all works now.I was under the impression that vars defined between the general <script> </script> were global to the page. I guess I was wrong.Thanks everybody for the assist. Guess I have a lot to learn about js too!

Link to comment
Share on other sites

Variables defined with var are in fact global to the page (assuming that they were defined in the global scope and not a local scope). You may have had something else using the same variable name and overwriting the array.

Link to comment
Share on other sites

Well you confirmed my understanding of this but it seems this isn't working for me. I swapped the var name, but same problem. I going to move on because all is working as I need and I have much more on my plate to screw up...Thanks again.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...