kadin
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>
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.
for (var expected in errInput[j]) {
?
It's not working for sure, but I don't know what you're trying to do.
ASKER
I am trying to get this to print zero two three. Or print what ever happens to be in the array.
ASKER
Maybe if I remove:
for (var expected in errInput[j]) {
and put
switch (errInput) {
for (var expected in errInput[j]) {
and put
switch (errInput) {
check this : http://jsfiddle.net/NfCwp/1/
<!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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
case 'zero': do this;
case one': do this;
case 'two': do this;
case 'three': do this;
ASKER
Thanks. How do I make them get the words?
ASKER
Sorry GeoffHarper. I did not see your post. Let me test your comment.
ASKER
Thank all of you for your help.
errInput[0] = 'zero';
//errInput[1] = 'one';
errInput[2] = 'two';
errInput[3] = 'three';
same as :
errInput = ["zero", "two", "three"];