?
Solved

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

Posted on 2011-10-24
12
Medium Priority
?
375 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:kadin
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 83

Expert Comment

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

same as :

errInput = ["zero", "two", "three"];
0
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 37019931
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.
0
 

Author Comment

by:kadin
ID: 37019960
I am trying to get this to print zero two three. Or print what ever happens to be in the array.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:kadin
ID: 37019969
Maybe if I remove:

 for (var expected in errInput[j]) {

 and put

switch (errInput) {
0
 
LVL 83

Expert Comment

by:leakim971
ID: 37020011
0
 
LVL 83

Expert Comment

by:leakim971
ID: 37020019
<!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

0
 
LVL 4

Accepted Solution

by:
GeoffHarper earned 1200 total points
ID: 37020045

<!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';

for (var j = 0; j < errInput.length; j++) {
	switch (errInput[j]) {
		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

0
 
LVL 13

Assisted Solution

by:haloexpertsexchange
haloexpertsexchange earned 800 total points
ID: 37020046
ok the reason that your case statements are not printing anything out is because expected never gets the words zero, one, two or three. As you have it standing right now it gets 0,1,2 or 3.
0
 

Author Comment

by:kadin
ID: 37020065
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;
0
 

Author Comment

by:kadin
ID: 37020088
Thanks. How do I make them get the words?
0
 

Author Comment

by:kadin
ID: 37020096
Sorry GeoffHarper. I did not see your post. Let me test your comment.
0
 

Author Closing Comment

by:kadin
ID: 37020114
Thank all of you for your help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question