functions, arrays, and loops, oh my!

They have: 7 posts

Joined: Nov 2001

I know what I need to do but I don't know how to get javascript to do it. But I think I'm headed in the right direction.

Please take a look at my code below and let me know:
1. Any obvious(syntax related) or not so obvious reasons why it doesn't work and if possible, how to fix it.
2. If I am completely off base for what I'm trying to accomplish.

Purpose of Code:
Basically, I have some layers on a page and I need the user to click on all of them before proceeding to the next page. When the user clicks the "next page" button, I need javascript to check and see if all the layers were clicked. If they were, the user is directed to the next page. If they weren't, an alert message tells the user to keep clicking layers. (This is needed as part of a test for training purposes.)

Code:

script language="javascript 1.2">
setLayer = new Array(2)
setLayer[0] = 0
setLayer[1] = 0
setLayer[2] = 0

function layerSelected (){
if (layer.id = 'answer1'){
setLayer[0] = 1
} else (layer.id = 'answer2'){
setLayer[1] = 1
} else (layer.id = 'answer3'){
setLayer[2] = 1
}
}

function checkLayers (){
for (var i = 0; i <= setLayer.length; i++){
if (setLayer[i] = 0){
alert ("Where else could you find this information?");
break;
}
window.location="next_page.htm"
}
</script>

TIA for being patient with a beginner... I am trying! Smiling

Have a great weekend Cheers!

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi htmler,

Your major problem is:

assignment versus comparison

Javascript (and other C-like languages) use the following conventions:

assignment -- "=" (one equal sign)
var x = 2;
comparison -- "==" (two equal signs)
if (x == 2) doSomething();

Second major problem:
using a variable without declaring it -- 'layer'
Because a div exists, doesn't mean the var 'layer' is known to a function. For that to happen, you must pass the div to the function --

'this' is a javascript keyword that points to whatever is being referenced, so in the first case, 'this' means the div that has 'answer1' as an id; in the second, it refers to the div that has 'answer2' as the id.

once you pass the div object (with the 'this' keyword), you need to have the function recognize it --

function layerSelected(layer)

now 'layer' is a declared & defined variable, so the browser can now interpret the following line:

if (layer.id == 'answer1') setLayer[0] = 1

btw: you can also rewrite the above lines by sending the id directly as in:

<div id="answer1" onClick="layerSelected(this.id)"></div>
function layerSelected(layerID)
if (layerId == 'answer1') setLayer[0] = 1
'

Next, if you want to redirect 'only if' all layers have been clicked, you need to set a boolean variable and check it before redirecting (see function below)

if (isGood) window.location="next_page.htm"

Finally, you could have written this line --

var setLayer = new Array(2)

as any of the following:

1) state 3 cells will be created; then fill them as you did (setLayer[0] = 0;....etc)

var setLayer = new Array(3);

2)leave the number of cells unknown; then fill them as you did (setLayer[0] = 0;....etc)

var setLayer = new Array();

3) declare & assign the array in one statement (no need to fill cells)

var setLayer = new Array(0,0,0);

Here's the completed code:
<script language='javascript'>
var setLayer = new Array(0,0,0);

function layerSelected(divID)
{
if (divID == 'answer1') setLayer[0] = 1
else if (divID == 'answer2') setLayer[1] = 1
else if (divID == 'answer3') setLayer[2] = 1
}

function checkLayers()
{
var isGood = true;
for (var i = 0; i <= setLayer.length; i++)
{

if (setLayer[i] == 0)
{
isGood = false;
alert ("Where else could you find this information?");
break;
}
}
if (isGood) window.location="next_page.htm"
}

Hope this explains your initial problems. You will probably have additional problems, dependent upon the specific browsers and code you use.

Vinny

Where the world once stood
the blades of grass cut me still

They have: 7 posts

Joined: Nov 2001

Vinny~

Your advice was fantastic. Thanks for taking the extra time to walk me through my code and even suggest alternatives. It makes learning much easier. The code worked fine, too. Learning javascript can be frustrating but replies such as yours are encouraging and very helpful. Besides books and websites, this is my only other resource for learning or seeking help, and I appreciate the time that you spent.

Danka

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi htmler,

Re:

Quote: Your advice was fantastic.

Easy on the accolades; they'll go to my head. Laughing out loud
If you found the above helpful, check out my site -- there may be things there you'd find equally helpful.

BTW: do you have a url where you are using this? I am slightly curious as to why an individual would have to click on all divs.

Vinny

Where the world once stood
the blades of grass cut me still

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.