By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

please check my functions syntax

P: n/a
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
Sep 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"mantrid" <ia********@virgin.netwrote in message
news:qY****************@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';
}
}
}

Sep 3 '06 #2

P: n/a

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'.

Sep 3 '06 #3

P: n/a

"web.dev" <we********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.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?
Sep 3 '06 #4

P: n/a
JRS: In article <qY****************@newsfe6-win.ntli.net>, dated Sun, 3
Sep 2006 13:41:42 remote, seen in news:comp.lang.javascript, mantrid
<ia********@virgin.netposted :
>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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 3 '06 #5

P: n/a
>
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.htmjscr maths, dates,
sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items,
links.
Sep 4 '06 #6

P: n/a
JRS: In article <sV*****************@newsfe6-gui.ntli.net>, dated Mon,
4 Sep 2006 02:09:28 remote, seen in news:comp.lang.javascript, mantrid
<ia********@virgin.netposted :

>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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.