/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Switch ain’t gettin’ switchy with it.

I have been searching and searching and finaly found where my problem was with my code. I’m new to javascript, and though with all my searching I can’t find any rules that contradict what I have written here, I have isolated that the problem is that this switch isn’t properly switching. X and U always come out 0. Can someone tell me why, please? I’ve simplified the code here:

[CODE]var A = 1;
var B = 0;
var C = 0;

switch (A)
{
case 1: switch (B)
{
case 1: switch (C)
{
case 1: var X = 31; break;
case 2: var X = 7; break;
case 3: var X = 10; break;
case 4: var X = 6; break;
case 5: var X = 16; break;
case 6: var X = 8; break;
default: var X = 7; U = 6; break;
} break;
case 2: switch (C)
{
case 1: var X = 3; break;
case 2: var X = 8; break;
case 3: var X = 12; break;
default: var X = 2; var U = 3; break;
} break;
default: var U = 2; break;
} break;
case 2: switch (B)
{
case 1: var X = 8; break;
case 2: var X = 14; break;
case 3: var X = 15; break;
case 4: var X = 6; break;
default: var X = 2; var U = 4; break;
} break;
case 3: var X = 18; break;
case 4: var X = 16; break;
case 5: var X = 11; break;
case 6: var X = 3; break;
default: break;
}
if (X > 0) {}
else var X = 0;
if (U > 0) {}
else var U = 0;[/CODE]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 17.2008 — Overuse of the var statement I'd say. This seems to work.var A = 1;
var B = 1;
var C = 0;
var X,U;
switch (A)
{
case 1: switch (B)
{
case 1: switch (C)
{
case 1: X = 31; break;
case 2: X = 7; break;
case 3: X = 10; break;
case 4: X = 6; break;
case 5: X = 16; break;
case 6: X = 8; break;
default: X = 7; U = 6; break;
} break;
case 2: switch (C)
{
case 1: X = 3; break;
case 2: X = 8; break;
case 3: X = 12; break;
default: X = 2; var U = 3; break;
} break;
default: U = 2; break;
} break;
case 2: switch (B)
{
case 1: X = 8; break;
case 2: X = 14; break;
case 3: X = 15; break;
case 4: X = 6; break;
default: X = 2; U = 4; break;
} break;
case 3: X = 18; break;
case 4: X = 16; break;
case 5: X = 11; break;
case 6: X = 3; break;
}
if (X <= 0) X = 0; // Makes more sense that your if
if (U <= 0) U = 0; // Makes more sense that your if
alert(X+"n"+U);
Copy linkTweet thisAlerts:
@sadanyagciauthorJul 17.2008 — Thank you. I'll try that out and let you know if it works on my page.

As for the change to my if statement, that won't work. I need it to assign 0 to the variable if it has no value, not if it has a value of 0 or less. Is there a better way to do that than my if statement? I didn't find anything like a "not greater than" opporator. It would be great if !> worked.
Copy linkTweet thisAlerts:
@sadanyagciauthorJul 17.2008 — I tried it and it's still not working. The alert you put in comes up with two "undefined"s.
Copy linkTweet thisAlerts:
@sadanyagciauthorJul 17.2008 — I don't get it. I can't get the thing to work. Shall I post a zip file of the photogallery page? It's got only a few small files, and I can at least tell you what is and isn't working.

[B]gallery.html[/B] - simple file that brings the scripts together - it works

[B]lightbox.js [/B]- to display the pictures - it works

[B]lightbox.css[/B] - it works

[B]parseQueryString.js [/B]- I coppied it from another page, and though it probably does a lot more than I need it to do, it does the job I want it to do. - it works

[B]variables.js [/B]- contains the introduction of the variables, and the script you see above. - [U]This seems to be where the problem is.[/U]

[B]references.js [/B]- shows where you are on the site, and links back to previous pages. - it works

[B]folders.js[/B] - shows the folders contained in the section of the site - Has it's own problems that I can't figure out. If it does get to run it comes up with an error saying it's making Internet Explorer slow and needs to be stopped. However, with this current problem it's not even getting to run.

[B]files.js[/B] - shows the files contained within the section of the site. - it seems to work


Shall I post the zip file or something? If so, where should I post it?
Copy linkTweet thisAlerts:
@Declan1991Jul 17.2008 — Do, zip all the files and attach it here (don't use the quick reply, and click on the paperclip). This page alerts 7 and 6, so your problem must be elsewhere.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var A = 1;
var B = 1;
var C = 0;
var X = 0,U = 0;
switch (A)
{
case 1: switch (B)
{
case 1: switch (C)
{
case 1: X = 31; break;
case 2: X = 7; break;
case 3: X = 10; break;
case 4: X = 6; break;
case 5: X = 16; break;
case 6: X = 8; break;
default: X = 7; U = 6; break;
} break;
case 2: switch (C)
{
case 1: X = 3; break;
case 2: X = 8; break;
case 3: X = 12; break;
default: X = 2; var U = 3; break;
} break;
default: U = 2; break;
} break;
case 2: switch (B)
{
case 1: X = 8; break;
case 2: X = 14; break;
case 3: X = 15; break;
case 4: X = 6; break;
default: X = 2; U = 4; break;
} break;
case 3: X = 18; break;
case 4: X = 16; break;
case 5: X = 11; break;
case 6: X = 3; break;
}
alert(X+"n"+U);
</script>
</head>
<body>

</body>
</html>
Copy linkTweet thisAlerts:
@slaughtersJul 17.2008 — OR - you just say *NO* to case statements and just use a multidimensional array (http://www.irt.org/script/365.htm) to store the values, where A, B, and C are indexes
Copy linkTweet thisAlerts:
@sadanyagciauthorJul 17.2008 — Well, solved both my problems. Thank you all for your help. Turns out my problem with X and U was that A, B, and C needed to be changed from strings to numbers. I didn't know the way I was importing them into the code turned them into strings. Anyway, that problem solved. Here's the solution, for all who care.
<i>
</i>var queryObj = parseQueryString( location.search );

var A = unescape(queryObj.A);
var B = unescape(queryObj.B);
var C = unescape(queryObj.C);

A = parseInt(A);
B = parseInt(B);
C = parseInt(C);

I just added the parseInt's and it started doing what it was made to do.


I solved my other problem as well. Turns out the folders section problem was caused by a misplaced semicolon after the while statement.

<i>
</i>if (U &gt; 0)
{
document.write("&lt;p align=center style='margin-top: 0; margin-bottom: 0'&gt; ");
while (V &lt;= U);
{
if (B &gt; 0)
{
document.write("&lt;a href=gallery.html?A=" + A + "&amp;B=" + B + "&amp;C=" + V + "&gt; ");
}
else
{
document.write("&lt;a href=gallery.html?A=" + A + "&amp;B=" + V + "&amp;C=0&gt; ");
}
document.write("&lt;img border=0 src=" + PICTURES + "00/03/01/fi" + FOLDERx + V + ".jpg ");
document.write("width=256 height=234&gt;&lt;/a&gt;&amp;nbsp; ");
V++;
}
document.write("&lt;/p&gt; ");
}

Notice the while (V <= U)[COLOR="Red"][B];[/B][/COLOR]

I don't know what I was telling it to do, but Internet Explorer sure had a cow with it! The thing kept lagging the computer and telling me it needed to end the script. I, being the nice guy that I am, obeyed at least half the time. Poor little computer.

Anyway, thank you for your help. If you want I can post the zip file anyway; but I'm sure you guys have enough codes to look at.
Copy linkTweet thisAlerts:
@sadanyagciauthorJul 18.2008 — I evaluated using an array for that section, but feel it would just be too much trouble to implement. Switch seems to work just fine for this application.

[upl-file uuid=86eaea18-8b94-42c9-a918-4c545651223a size=7kB]photogallery.zip[/upl-file]
×

Success!

Help @sadanyagci spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...