Link to home
Start Free TrialLog in
Avatar of kadin
kadinFlag for United States of America

asked on

What's wrong with my JavaScript for loop, please.

I cannot get this to work or produce an error in firebug. Thanks.


<?php

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>for loop</title>
   
<script language="javascript" type="text/javascript">

var errInput = Array(4);
errInput[0] = 'zero';
//errInput[1] = 'one';
errInput[2] = 'two';
errInput[3] = 'three';

var expected = Array(4);
for (var j = 0; j < errInput.length; j++) {
	for (var expected in errInput[j]) {
					
		switch (expected) {
			case 'zero': document.write('zero'+'<br>'); break;
			case 'one': document.write('one'+'<br>'); break;
			case 'two': document.write('two'+'<br>'); break;
			case 'three': document.write('three'+'<br>'); break;
		}
	}
}

</script>
</head>
</html>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

var errInput = Array(4);
errInput[0] = 'zero';
//errInput[1] = 'one';
errInput[2] = 'two';
errInput[3] = 'three';

same as :

errInput = ["zero", "two", "three"];
What are you trying to do with the line:

      for (var expected in errInput[j]) {

?

It's not working for sure, but I don't know what you're trying to do.
Avatar of kadin

ASKER

I am trying to get this to print zero two three. Or print what ever happens to be in the array.
Avatar of kadin

ASKER

Maybe if I remove:

 for (var expected in errInput[j]) {

 and put

switch (errInput) {
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>for loop</title>   
</head>
<body>
<script language="javascript" type="text/javascript">

var errInput = new Array(4);
errInput[0] = 'zero';
//errInput[1] = 'one';
errInput[2] = 'two';
errInput[3] = 'three';

for (var j = 0; j < errInput.length; j++) {
    document.write(errInput[j]);
    document.write('<br />');    
}

</script>
    
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GeoffHarper
GeoffHarper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kadin

ASKER

Maybe I should of pointed out that I need the switch statement because depending on what is in the array, if just one item in the array it will just do one. If one and three are in the array it will do one and three.

case 'zero': do this;
case one': do this;
case 'two': do this;
case 'three': do this;
Avatar of kadin

ASKER

Thanks. How do I make them get the words?
Avatar of kadin

ASKER

Sorry GeoffHarper. I did not see your post. Let me test your comment.
Avatar of kadin

ASKER

Thank all of you for your help.