f



please check my functions syntax

Hello
can anyone see why the following function is not working.
The extended version, where I take out the first 'for' loop and substitute
in all the 'cases' works.
Sorry if I'm being dumb but Im still learning javascript

***************************************
function answernum() {
var selnumber =
document.questform.answno.options[document.questform.answno.selectedIndex].v
alue;

switch (selnumber){
     for(var n = 2; n <= 10; n++){
     case n:
          for(var x = 7; x <= 7++; x++){
               var answerrow = document.getElementById("row"+x);
               answerrow.style.display='';
          }
     break
     }
    default:
         for(var x = 7; x <= 15; x++){
              var answerrow = document.getElementById("row"+x);
              answerrow.style.display='none';
         }
    }
}
*************************************

Thanks
Once again
Ian


0
mantrid
9/3/2006 1:41:42 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

7 Replies
173364 Views

Similar Articles

[PageSpeed] 59

"mantrid" <ian.dandav@virgin.net> wrote in message
news:qYAKg.2084$Mh2.386@newsfe6-win.ntli.net...
> Hello
> can anyone see why the following function is not working.
> The extended version, where I take out the first 'for' loop and substitute
> in all the 'cases' works.
> Sorry if I'm being dumb but Im still learning javascript
>
> ***************************************
> function answernum() {
> var selnumber =
>
document.questform.answno.options[document.questform.answno.selectedIndex].v
> alue;
>
> switch (selnumber){
>      for(var n = 2; n <= 10; n++){
>      case n:
>           for(var x = 7; x <= 7++; x++){
>                var answerrow = document.getElementById("row"+x);
>                answerrow.style.display='';
>           }
>      break
>      }
>     default:
>          for(var x = 7; x <= 15; x++){
>               var answerrow = document.getElementById("row"+x);
>               answerrow.style.display='none';
>          }
>     }
> }
> *************************************
>
> Thanks
> Once again
> Ian
>
>

If it helps the working function is below


function answernum() {
var selnumber =
document.questform.answno.options[document.questform.answno.selectedIndex].v
alue;

switch (selnumber){
 case '2':
  for(var x = 7; x <= 7; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '3':
  for(var x = 7; x <= 8; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '4':
  for(var x = 7; x <= 9; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '5':
  for(var x = 7; x <= 10; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '6':
  for(var x = 7; x <= 11; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '7':
  for(var x = 7; x <= 12; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '8':
  for(var x = 7; x <= 13; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '9':
  for(var x = 7; x <= 14; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '10':
  for(var x = 7; x <= 15; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 default:
  for(var x = 7; x <= 15; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='none';
  }
 }
}



0
mantrid
9/3/2006 1:45:02 PM
JRS:  In article <qYAKg.2084$Mh2.386@newsfe6-win.ntli.net>, dated Sun, 3
Sep 2006 13:41:42 remote, seen in news:comp.lang.javascript, mantrid
<ian.dandav@virgin.net> posted :
>Hello
>can anyone see why the following function is not working.
>The extended version, where I take out the first 'for' loop and substitute
>in all the 'cases' works.
>Sorry if I'm being dumb but Im still learning javascript
>
>***************************************
>function answernum() {
>var selnumber =
>document.questform.answno.options[document.questform.answno.selectedIndex].v
>alue;
>
>switch (selnumber){
>     for(var n = 2; n <= 10; n++){
>     case n:
>          for(var x = 7; x <= 7++; x++){
>               var answerrow = document.getElementById("row"+x);
>               answerrow.style.display='';
>          }
>     break
>     }
>    default:
>         for(var x = 7; x <= 15; x++){
>              var answerrow = document.getElementById("row"+x);
>              answerrow.style.display='none';
>         }
>    }
>}

You're evidently writing far too much code at once in the pious hope
that it can be understood, then trying to fond out why it cannot.

And 7++ cannot be right : code should be copy'n'pasted, not manually
transcribed.

If one could tell what you actually want, one could probably suggest a
much better way to do it.


function answernum() { var x, n, answerrow 
  var selnumber = + document.questform.answno. 
    options[document.questform.answno.selectedIndex].value
        // selnumber will now be a Number not a String.
  for (x = 7; x <= 15; x++) {
    answerrow = document.getElementById("row"+x)
    answerrow.style.display = (x > selnumber) ? '' : 'none'
    }
  }

may be more like what you need.

Always write code in pieces small enough that they are probably correct,
and then test the pieces before writing more.

Your function would be easier to test if selnumber were a parameter, and
the loop body were replaced by
        alert("row" + x + "   " + ( (x > selnumber) ? '' : 'none' ) )
as you would then be testing the management and manipulation parts
independently.

-- 
 � John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 �
 <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript 
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0
Dr
9/3/2006 7:19:33 PM
mantrid wrote:
> switch (selnumber){
>      for(var n = 2; n <= 10; n++){

[snip]

>      }
>     }
> }

That is because you have an invalid syntax.  You don't place a 'for
loop' right after a switch statement.  What follows should be the
cases.  Within the cases you can have 'for loops'.

0
web
9/3/2006 7:41:36 PM
"web.dev" <web.dev.cs@gmail.com> wrote in message
news:1157312496.122999.68750@b28g2000cwb.googlegroups.com...
>
> mantrid wrote:
> > switch (selnumber){
> >      for(var n = 2; n <= 10; n++){
>
> [snip]
>
> >      }
> >     }
> > }
>
> That is because you have an invalid syntax.  You don't place a 'for
> loop' right after a switch statement.  What follows should be the
> cases.  Within the cases you can have 'for loops'.
>

the cases are being generated by the for loop. so they are there, arnt they?


0
mantrid
9/3/2006 8:30:28 PM
mantrid said:
>
>
>"web.dev" <web.dev.cs@gmail.com> wrote in message
>news:1157312496.122999.68750@b28g2000cwb.googlegroups.com...
>>
>> mantrid wrote:
>> > switch (selnumber){
>> >      for(var n = 2; n <= 10; n++){
>>
>> [snip]
>>
>> >      }
>> >     }
>> > }
>>
>> That is because you have an invalid syntax.  You don't place a 'for
>> loop' right after a switch statement.  What follows should be the
>> cases.  Within the cases you can have 'for loops'.
>>
>
>the cases are being generated by the for loop. so they are there, arnt they?

No.  The cases cannot be generated. 


-- 
                         
0
Lee
9/3/2006 10:27:33 PM
>
> You're evidently writing far too much code at once in the pious hope
> that it can be understood, then trying to fond out why it cannot.
>
> And 7++ cannot be right : code should be copy'n'pasted, not manually
> transcribed.

it was copy and pasted. the ++7 was my attempt to find a way of incrementing
the upper x value by one for successive cases
ie

case '2':
  for(var x = 7; x <= 7; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '3':
  for(var x = 7; x <= 8; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break
 case '4':
  for(var x = 7; x <= 9; x++){
   var answerrow = document.getElementById("row"+x);
   answerrow.style.display='';
  }
 break

see my second post. which shows a working version, abeit long and un elegant



> If one could tell what you actually want, one could probably suggest a
> much better way to do it.

simply put I have 10 text fields on separate rows identified by a <div>.
selecting 1 to 10 from a dropdownlist will determine which are shown
eg
select 1 from list and div1 (and its text field) is revealed
select 2 from list and div1 and div2 (and their text fields) are revealed
select 3 from list and div1, div2 and div3 (and their text fields) are
revealed
etc upto 10 divs


> function answernum() { var x, n, answerrow
>   var selnumber = + document.questform.answno.
>     options[document.questform.answno.selectedIndex].value
>         // selnumber will now be a Number not a String.
>   for (x = 7; x <= 15; x++) {
>     answerrow = document.getElementById("row"+x)
>     answerrow.style.display = (x > selnumber) ? '' : 'none'
>     }
>   }
>
> may be more like what you need.


didnt work, cant see where the variable n is being used

>
> Always write code in pieces small enough that they are probably correct,
> and then test the pieces before writing more.


I did, with the original with a simple 'case' which worked (see my second
post). I extended it and it still worked.
Problems started when I tried to compact the code into a more elegant form.

>
> Your function would be easier to test if selnumber were a parameter, and
> the loop body were replaced by
>         alert("row" + x + "   " + ( (x > selnumber) ? '' : 'none' ) )
> as you would then be testing the management and manipulation parts
> independently.


will try this tommorrow
thanks for the help

is it true that you cannot generate all the cases in a switch with a for
loop? another poster pointed this out to me. I cant see why this would be so

Ian

>
> -- 
>  � John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE
4 �
>  <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of
news:comp.lang.javascript
>  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
sources.
>  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,
links.


0
mantrid
9/4/2006 2:09:28 AM
JRS:  In article <sVLKg.9151$DB3.3194@newsfe6-gui.ntli.net>, dated Mon,
4 Sep 2006 02:09:28 remote, seen in news:comp.lang.javascript, mantrid
<ian.dandav@virgin.net> posted :


>> If one could tell what you actually want, one could probably suggest a
>> much better way to do it.
>
>simply put I have 10 text fields on separate rows identified by a <div>.
>selecting 1 to 10 from a dropdownlist will determine which are shown
>eg
>select 1 from list and div1 (and its text field) is revealed
>select 2 from list and div1 and div2 (and their text fields) are revealed
>select 3 from list and div1, div2 and div3 (and their text fields) are
>revealed
>etc upto 10 divs

You've not said whether, on selecting 3 then 1, div2 and div3 should
disappear : I assume so.

>> function answernum() { var x, n, answerrow
>> ...
>didnt work, cant see where the variable n is being used

Note the "more like".  You need to understand it, and use something
similar adapted to your actual needs.

This complete code, for IE4 and compatible, hides/shows DIVs in the
manner that you seem to want.

<input ID=N type=text>
<input type=button onClick = "answernum(+N.value)">
<div ID=A0>A0</div>
<div ID=A1>A1</div>
<div ID=A2>A2</div>
<div ID=A3>A3</div>

<script>

function answernum(selnumber) { var x, answerrow
  for (x = 0; x <= 3; x++) {
    answerrow = document.all["A"+x]
    answerrow.style.display = (x >= selnumber) ? '' : 'none'
    }
  }
  
</script>


Or :

function answernum(selnumber) { var x = 4
  while (x--)
    document.all["A"+x].style.display = (x >= selnumber) ? '' : 'none' }


>is it true that you cannot generate all the cases in a switch with a for
>loop? another poster pointed this out to me. I cant see why this would be so

He was indicating that you so not understand the grammar of switch-case.
A switch is for selecting a single case, though it can be made to select
more.

>> -- 
>> ...

Don't overquote; don't quote signatures.


It's a good idea to read the newsgroup and its FAQ.
-- 
 � John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 �
 <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript 
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0
Dr
9/4/2006 8:00:00 PM
Reply: