473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Sep 3 '06 #1
6 1366

"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

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

"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
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
4
by: gs | last post by:
I have searched Google, MSDN,... for a week. I am still unable to make available functions in my csharp dll as native windows functions for some legacy non dotnet application I just want to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
by: BabyBlue | last post by:
I have a function like this: function get_articles($cat=1,$numberposts=1); it can be used: get_articles(cat=4&numberposts=6); //will display 6 posts from category 6 then I want to use that...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
2
by: benicio | last post by:
The subject update failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = 6' at line 5. This is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.