Google Answers Logo
View Question
 
Q: JavaScript --- Need help!! ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: JavaScript --- Need help!!
Category: Computers > Programming
Asked by: jellybeans-ga
List Price: $20.00
Posted: 20 Oct 2004 19:14 PDT
Expires: 19 Nov 2004 18:14 PST
Question ID: 417850
Hello!  

I need help in modifying a JavaScript function that I have currently.

Imagine the following:

Suppose we have a table of 9 levels, with values corresponding to
those levels, say:

*LEVEL*      *VALUE RANGE (FROM)*     *VALUE RANGE (TO)*

Level 1      0                       14
Level 2      15                      24
Level 3      25                      35
Level 4      36                      46
Level 5      47                      49
Level 6      50                      64
Level 7      65                      74
Level 8      75                      84
Level 9      85                      

Suppose that a user provides a number, and we want to determine which
"level" that number falls in.

First of all, if the number is greater than or equal to the FROM range
of level 9, then the level is automatically equal to 9 (in other
words, any positive integer no matter how large, the script will
always return a level between 1 and 9)

If the supplied number is less than the "FROM" range value of level 9,
then the level would be 8. For example, if the user provided the
number 77, the script knows that it's less than the FROM range of
level 9 so therefore it sets the level as 8.

If the supplied number is less than the "FROM" range value of level 8,
then the level would be 7.

If the supplied number is less than "FROM" range value of level 7,
then we're in level 6.

I think you can see the general idea here...

Now, here's the critical thing:

I need a script that will determine the correct level given a supplied
value by the user even when some of the latter levels may take no
value (i.e., blank).

For example, if level 9 was blank, and the user supplied 78, what
would the script compare 78 to so that it could determine that the
level was 8? (it can't compare it to level 9 because it's blank.....).

But that's exactly my question. I need some sort of an innovative
approach so that it can figure out the level even if it's dealing with
level(s) that are blank. Evidently, when JavaScript compares a value
to a blank it trips up.

One assumption you can make here with my data is that level 1 will
NEVER be blank. Level 2 and above might, though.

Again, it is critical that you provide a script that can determine the
level even when it's comparing values to a level that might be blank.
I do not want anything automatically filled on the screen showing
actual values in levels that are denoted as being blank. They need to
remain as such. The calculations and determinations need to be made
'behind the scenes' as it were :)

Also, all I need is the general logic that I'd need in implementing
such a script, not necessarily a full blown script that prints or
writes anything to the screen. I can figure that out myself.

Thanks!   (generous tip offered)
Answer  
Subject: Re: JavaScript --- Need help!!
Answered By: hammer-ga on 21 Oct 2004 07:54 PDT
Rated:5 out of 5 stars
 
Jellybeans,

I'm not sure exactly what your logic needs to be, but you are asking
for an example of technique, so I've provided a small form. The source
is below, and is also available to play with at
http://www.hammerdata.com/Google/jellybeans.html .

The technique demonstrates filling your range values into an array,
filtering for blanks as you go. You can then compare your test value
to the filtered values, without having to alter anything on screen.
There are more elegant ways to fill arrays, but I wanted the technique
to be more clear than effcient, so you could see what's going on. You
indicate that you have some knowledge of JavaScript, so I figured that
you could manage efficiency once you knew what to do.

This technique does assume that the FROM values always increase as you
move up the levels.

Please let me know if you need clarification on this.

Good luck with your project!

- Hammer

-----------------------------------

<HTML>
<HEAD>
<script language="javascript">
function CheckIt()
{
var ranges = new Array();
var inrange = '';
var inval = 0;
var range = -1;

        // Populate Range Array
        // setting the blank ranges to -1
        inrange = document.xform.range1.value;
        if(inrange == '') ranges[0] = -1
        else ranges[0] = inrange - 0;
        inrange = document.xform.range2.value;
        if(inrange == '') ranges[1] = -1
        else ranges[1] = inrange - 0;
        inrange = document.xform.range3.value;
        if(inrange == '') ranges[2] = -1
        else ranges[2] = inrange - 0;
        inrange = document.xform.range4.value;
        if(inrange == '') ranges[3] = -1
        else ranges[3] = inrange - 0;
        inrange = document.xform.range5.value;
        if(inrange == '') ranges[4] = -1
        else ranges[4] = inrange - 0;
        inrange = document.xform.range6.value;
        if(inrange == '') ranges[5] = -1
        else ranges[5] = inrange - 0;
        inrange = document.xform.range7.value;
        if(inrange == '') ranges[6] = -1
        else ranges[6] = inrange - 0;
        inrange = document.xform.range8.value;
        if(inrange == '') ranges[7] = -1
        else ranges[7] = inrange - 0;
        inrange = document.xform.range9.value;
        if(inrange == '') ranges[8] = -1
        else ranges[8] = inrange - 0;

        // Get the compare value
        inval = document.xform.myval.value - 0;

        // Run through the range array. If a range
        // is -1 (blank) it is not compared. Otherwise
        // if the value is above the minimum for that
        // range, the range marker moves up.
        for(var j = 0;j < 9;j++)
        {
                if(ranges[j] > -1)
                {
                        if(inval >= ranges[j]) range = j;
                }
        }

        if(range > -1)
        {
                // Increment to show range number instead
                // of array index.
                range++;
                alert('Range: ' + range);
        }
        else
        {
                // Found no valid range. Throw an error.
                alert('Cannot determine range.');
        }
}
</script>
</HEAD>
<BODY>
<FORM name="xform" onSubmit="CheckIt();">
Range 1: <input type="text" name="range1" value="0"><BR>
Range 2: <input type="text" name="range2" value="5"><BR>
Range 3: <input type="text" name="range3" value="10"><BR>
Range 4: <input type="text" name="range4" value="15"><BR>
Range 5: <input type="text" name="range5" value="20"><BR>
Range 6: <input type="text" name="range6" value="25"><BR>
Range 7: <input type="text" name="range7" value="30"><BR>
Range 8: <input type="text" name="range8" value="35"><BR>
Range 9: <input type="text" name="range9" value="40"><BR>
Check Value: <input type="text" name="myval">
<input type="submit" name="go" value="Run Me">
</FORM>
</BODY>
</HTML>

Request for Answer Clarification by jellybeans-ga on 21 Oct 2004 16:35 PDT
Hey Hammer!

Thanks for your very clever answer. I have checked out the link you
provided and it appears to do the job well.  Excellent stuff.

I have some familiarity with JS, but I'm no expert.  I was wondering
if you can clarify how I can actually edit your script to fit in with
mine.

In a nutshell, here is how I am currently determining my level:

if ( parseInt(form.Rounded.value) >= parseInt(form.Start9.value) )
form.Level.value = 9

if ( parseInt(form.Rounded.value) < parseInt(form.Start9.value) )
form.Level.value = 8

if ( parseInt(form.Rounded.value) < parseInt(form.Start8.value) )
form.Level.value = 7

if ( parseInt(form.Rounded.value) < parseInt(form.Start7.value) )
form.Level.value = 6

if ( parseInt(form.Rounded.value) < parseInt(form.Start6.value) )
form.Level.value = 5

if ( parseInt(form.Rounded.value) < parseInt(form.Start5.value) )
form.Level.value = 4

if ( parseInt(form.Rounded.value) < parseInt(form.Start4.value) )
form.Level.value = 3

if ( parseInt(form.Rounded.value) < parseInt(form.Start3.value) )
form.Level.value = 2

if ( parseInt(form.Rounded.value) < parseInt(form.Start2.value) )
form.Level.value = 1


To match it up with your notation,  I'm using  Start1 as the first
range, Start2 as the second range, etc, etc..

parseInt(form.Rounded.value) above would equate with the supplied
value in your script which is set at "inval".

I tried modifying your script somewhat to accomodate for my
form.Level.value and such, but got all sorts of errors.  I think I'm
doing something wrong.

Can you help me tweak it so that it's along the same lines as what you've done?

To get an idea of what I attempted to do (receiving script errors)
here's what it was: (I have left out the standard javascript tags and
end tags here and will include only the guts).   Before I paste the
script in, I don't know whether my line which starts "form.Level.value
= range;"  is valid or not..

// BEGIN NEW CODE


var ranges = new Array();
var inrange = '';
var inval = 0;
var range = -1;

        // Populate Range Array
        // setting the blank ranges to -1
        inrange = parseInt(form.Start1.value);
        if(inrange == '') ranges[0] = -1
        else ranges[0] = inrange - 0;
        inrange = parseInt(form.Start2.value);
        if(inrange == '') ranges[1] = -1
        else ranges[1] = inrange - 0;
        inrange = parseInt(form.Start3.value);
        if(inrange == '') ranges[2] = -1
        else ranges[2] = inrange - 0;
        inrange = parseInt(form.Start4.value);
        if(inrange == '') ranges[3] = -1
        else ranges[3] = inrange - 0;
        inrange = parseInt(form.Start5.value);
        if(inrange == '') ranges[4] = -1
        else ranges[4] = inrange - 0;
        inrange = parseInt(form.Start6.value);
        if(inrange == '') ranges[5] = -1
        else ranges[5] = inrange - 0;
        inrange = parseInt(form.Start7.value);
        if(inrange == '') ranges[6] = -1
        else ranges[6] = inrange - 0;
        inrange = parseInt(form.Start8.value);
        if(inrange == '') ranges[7] = -1
        else ranges[7] = inrange - 0;
        inrange = parseInt(form.Start9.value);
        if(inrange == '') ranges[8] = -1
        else ranges[8] = inrange - 0;

        // Get the compare value
        inval = parseInt(form.Rounded.value) - 0;

        // Run through the range array. If a range
        // is -1 (blank) it is not compared. Otherwise
        // if the value is above the minimum for that
        // range, the range marker moves up.
        for(var j = 0;j < 9;j++)
        {
                if(ranges[j] > -1)
                {
                        if(inval >= ranges[j]) range = j;
                }
        }

        if(range > -1)
        {
                // Increment to show range number instead
                // of array index.
                range++;
                form.Level.value = range;
                
        }
        else
        {
                // Found no valid range. Throw an error.
                alert('Cannot determine range.');
        }
}

// END NEW CODE TO FIGURE OUT LEVEL


Thanks!

jellybeans

Clarification of Answer by hammer-ga on 22 Oct 2004 04:52 PDT
Jellybeans,

You need to get all the parseInt calls out of there. My example
assumes things are coming in from the form as text and does all the
necessary conversions. Subtracting zero from a String converts it to
an Int. By using parseInt, you are doing the conversions early, so the
comparisons to string values like '' are failing. Since I seem to have
guessed correctly regarding where your values are coming from, all you
should need to do is replace my form field references with yours.
Since I don't have your form, I'm assuming that your form name and
field names are correct in your code. Something like this:

function CheckIt()
{
var ranges = new Array();
var inrange = '';
var inval = 0;
var range = -1;

        // Populate Range Array
        // setting the blank ranges to -1
        inrange = document.form.Start1.value;
        if(inrange == '') ranges[0] = -1
        else ranges[0] = inrange - 0;
        inrange = document.form.Start2.value;
        if(inrange == '') ranges[1] = -1
        else ranges[1] = inrange - 0;
        inrange = document.form.Start3.value;
        if(inrange == '') ranges[2] = -1
        else ranges[2] = inrange - 0;
        inrange = document.form.Start4.value;
        if(inrange == '') ranges[3] = -1
        else ranges[3] = inrange - 0;
        inrange = document.form.Start5.value;
        if(inrange == '') ranges[4] = -1
        else ranges[4] = inrange - 0;
        inrange = document.form.Start6.value;
        if(inrange == '') ranges[5] = -1
        else ranges[5] = inrange - 0;
        inrange = document.form.Start7.value;
        if(inrange == '') ranges[6] = -1
        else ranges[6] = inrange - 0;
        inrange = document.form.Start8.value;
        if(inrange == '') ranges[7] = -1
        else ranges[7] = inrange - 0;
        inrange = document.form.Start9.value;
        if(inrange == '') ranges[8] = -1
        else ranges[8] = inrange - 0;

        // Get the compare value
        inval = document.form.Rounded.value - 0;

        // Run through the range array. If a range
        // is -1 (blank) it is not compared. Otherwise
        // if the value is above the minimum for that
        // range, the range marker moves up.
        for(var j = 0;j < 9;j++)
        {
                if(ranges[j] > -1)
                {
                        if(inval >= ranges[j]) range = j;
                }
        }

        if(range > -1)
        {
                // Increment to show range number instead
                // of array index.
                range++;
                document.form.Level.value = range;
        }
        else
        {
                // Found no valid range. Throw an error.
                alert('Cannot determine range.');
        }
}

If you can't get this working, please provide me with a full web page
to look at. Javascript is very sensitive to syntax errors,
misspellings, etc. It's hard to debug without having all the pieces.

Additional Information:
-----------------------
By the way, there is an excellect JavaScript FAQ at irt.org.
IRT Javascript FAQ
http://developer.irt.org/script/script.htm

-- Hammer
jellybeans-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Thanks, hammer!

You're a genius.  The script does the job absolutely perfectly. Thanks
again, and I admire how you understood right from the outset what it
was I wanted.

Regards.

Comments  
Subject: Re: JavaScript --- Need help!!
From: binary_zero-ga on 20 Oct 2004 22:20 PDT
 
can you please list one table having blank level at some where and one
value which user enters.

i hope so i can help :)

regards

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy