1 Replies - 3125 Views - Last Post: 15 September 2012 - 02:57 PM

#1 izic  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 197
  • Joined: 15-January 09

Tracing some code

Posted 15 September 2012 - 12:01 PM

Hi, I just got through trying this code and the output I got was Eanie, Meany, Meany, Meany. However, when I test it on w3schools, Eanie Meany Meany Meany Meany is the correct output, why is that? Var i only ever gets to a value of 4 which means that based on this code it should print Meany three times.

<HTML><HEAD><TITLE></TITLE></HEAD>
<BODY>
<script LANGUAGE=Javascript>
for( var i=1; i < 5; ++i )
switch (i){
case 1:
document.write( "Eanie ");
case 2: case 4: case 3:
document.write("Meany ");
break;
default:
document.write("Miney Moe<br>");
}
</SCRIPT>
</BODY></HTML> 


Is This A Good Question/Topic? 0
  • +

Replies To: Tracing some code

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: Tracing some code

Posted 15 September 2012 - 02:57 PM

I wouldn’t concern myself with test results from w3schools. what counts is the browsers you and your users use.

besides that, w3schools is formally right. since you don’t have a break statement after the first case, it would fall through and print the first of the four Meanies. though there might be differences in browsers interpreting those cases.

This post has been edited by Dormilich: 15 September 2012 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1