Need help debugging a javascript?

JavaScript --- Need help!!

  • 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:

    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>

jellybeans-ga at Google Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.