Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What am I doing wrong with this Javascript function?

Here's what I've got:

function show_visibility(form,row,count) {
    var_ticker=0;
    var numRows = form.allocatedlistcount.value;
    //alert(numRows);
    //at this point, I'm blowing through all the rows to make sure no one is trying to allocate more than what's there
     for( var rowIndex = 1; rowIndex <= numRows; rowIndex++ ) {
        eval('comparison = form.total_cost_comparison_'+row+'.value;');
        eval('user_value = form.allocated_'+row+'.value;');
        user_value = Math.round(user_value*10000)/10000;
        comparison = Math.round(comparison*10000)/10000;
            if(user_value>comparison)
            {
            var_ticker=var_ticker+1;
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'red\';')  
            eval('document.getElementById(\'save_cell\').style.visibility = \'hidden\';'); 
            }
            else
            {
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'#656363\';')  
            eval('document.getElementById(\'save_'+row+'\').style.visibility = \'visible\';');
            }
        }
       //I have finished my loop. Now I want to see if my ticker increased to a value greater than 0, and if so, than I display my save button and do my total. Otherwise, I don't
       alert(var_ticker);
            if(var_ticker==0)
            {
            eval('document.getElementById(\'save_cell\').style.visibility = \'visible\';'); 
            addAllocated(form);
            }
            else
            {
           // alert("Your allocated amount cannot exceed what's been budgeted.");
            }
  }

Open in new window


Hopefully you can tell by the comments what I'm trying to do. Bottom line is that I want to loop through all the rows to see if my user is attempting to insert a value (user_value) that's greater than the system's value (comparison_value).

By inserting a ticker (var_ticker), I'm trying to figure out if any of the rows triggered a flag (if(user_value>comparison)). If that happened at any given point, then the trigger is increased by a value of 1.

Once past the loop, I look to see if that trigger is equal to zero. If so, I display the total. If it's greater than zero, I want to set the visibility of the save button to "hidden" etc.

The problem is that if even just one of the rows triggers  a problem, my ticker's value is the number of rows. It's not the number of times there was a user value greater than the system's value, rather it's the number of rows in the table.

Bottom line is that it doesn't work. I can enter in a value that's greater than the system's value, get the warning and then do the same thing on another row without fixing the discrepancy. I now have two flawed values. I'm OK in that the "save" button doesn't show, but if I change one of those values so it's OK but leave the other one alone, I still get the save button which shouldn't be the case and I'm thinking it's that ticker.

Thoughts?
Avatar of Sar1973
Sar1973
Flag of Italy image

At line 9 and 10, try to change the name of the variable (since it is the same of those calculated) from user_value = Math.round(user_value... to  var myUser_value = Math.round(user_value... and so after these lines.
Avatar of Bruce Gust

ASKER

Hey, Sar!

Here's what I did:

function show_visibility(form,row,count) {
    var var_ticker=0;
    var numRows = form.allocatedlistcount.value;
    //alert(numRows);
    //at this point, I'm blowing through all the rows to make sure no one is trying to allocate more than what's there
     for( var rowIndex = 1; rowIndex <= numRows; rowIndex++ ) {
        eval('comparison = form.total_cost_comparison_'+row+'.value;');
        eval('user_value = form.allocated_'+row+'.value;');
        var myUser_value = Math.round(user_value*10000)/10000;
        var myComparison = Math.round(comparison*10000)/10000;
            if(myUser_value>myComparison)
            {
            var_ticker=var_ticker+1;
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'red\';')  
            eval('document.getElementById(\'save_cell\').style.visibility = \'hidden\';'); 
            }
            else
            {
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'#656363\';')  
            eval('document.getElementById(\'save_'+row+'\').style.visibility = \'visible\';');
            }
        }
       //I have finished my loop. Now I want to see if my ticker increased to a value greater than 0, and if so, than I display my save button and do my total. Otherwise, I don't
       alert(var_ticker);
            if(var_ticker==0)
            {
            eval('document.getElementById(\'save_cell\').style.visibility = \'visible\';'); 
            addAllocated(form);
            }
            else
            {
           // alert("Your allocated amount cannot exceed what's been budgeted.");
            }
  }

Open in new window


My biggest concern is my var_ticker. It should only be increased by 1 if the myUser_value is greater than myComparison. At this point, it doesn't seem to matter whether or not it's greater or less, I get the number of rows and not the number of "greater than's."  What am I doing wrong with my code that my ticker isn't incrementing based on the if statement? Rather, it's simply recording the number of rows?
At line 13: replace var_ticker=var_ticker+1; with var_ticker++;
Now my alert reads "0" every time.

I'm still blowing it somewhere...
I may have misread your suggestion. In any event, here's the corrected code and my alert still reads "3"

function show_visibility(form,row,count) {
    var var_ticker=0;
    var numRows = form.allocatedlistcount.value;
    //alert(numRows);
    //at this point, I'm blowing through all the rows to make sure no one is trying to allocate more than what's there
     for( var rowIndex = 1; rowIndex <= numRows; rowIndex++ ) {
        eval('comparison = form.total_cost_comparison_'+row+'.value;');
        eval('user_value = form.allocated_'+row+'.value;');
        var myUser_value = Math.round(user_value*10000)/10000;
        var myComparison = Math.round(comparison*10000)/10000;
            if(myUser_value>myComparison)
            {
            var_ticker++;
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'red\';')  
            eval('document.getElementById(\'save_cell\').style.visibility = \'hidden\';'); 
            }
            else
            {
            eval('document.getElementById(\'view_comparison_'+row+'\').style.color = \'#656363\';')  
            eval('document.getElementById(\'save_'+row+'\').style.visibility = \'visible\';');
            }
        }
       //I have finished my loop. Now I want to see if my ticker increased to a value greater than 0, and if so, than I display my save button and do my total. Otherwise, I don't
       alert(var_ticker);
            if(var_ticker==0)
            {
            eval('document.getElementById(\'save_cell\').style.visibility = \'visible\';'); 
            addAllocated(form);
            }
            else
            {
           // alert("Your allocated amount cannot exceed what's been budgeted.");
            }
  }

Open in new window

It just means that the condition you have set stops after 3 steps; what should be wrong with this...?
I'm trying to establish the ticker as an indicator as to how many of my rows contained a situation where the user_value was larger than my comparison_value. If there's only one instance, I want my ticker to say "1." If there were two instances, I want it to read "2." Right now, apparently I've got it set up where it's just telling me how many times it looped through my code. I need it to tell me how many problems there were.
Can you tell me how many rows you have...? If you have 3 alerts, what should be wrong?
For example, I'll have one row where there should be an alert and my ticker reads "3." I'll go back and intentionally create another "bad" row so there should now be two alerts and instead I get 3. I never get a value on my ticker that reflects the number of "bad" rows, only the total number of rows.
So 3 is the total number of rows...? And why do you round to 10.000 your result?
Because in the process of passing variables from PHP to JavaScript, I had to cheat in order to get JavaScript to recognize it as an integer and not a string.

But do you see my dilemma in that I don't want the total number of rows, only the total number of rows where the user_value was larger than my comparison value?
It is clear to me: I was just trying to understand if this result of 3 alerts (ticker) may be compatible with the total number of rows the code has exhamined.
It is. With the example I'm working with I've got three rows, no more. But I don't necessarily have three rows with a user_value larger than my comparison_value and that's the poison I'm trying to eliminate.
But (since you have so few rows) are you able to say if they all match your criteria or not...? Remember that in the aval function you have set variables that compare with the criteria  form.total_cost_comparison_'+row+'.value;'<form.allocated_'+row+'.value
I can tell whether or not they match my criteria or not, but here's something I want you to see, because in an effort to hone in on what the problem is, I put a little sandbox together and wrote this:

function show_visibility(form, row, count) {
var var_ticker=0;
var numRows = form.allocatedlistcount.value;

	for( var rowIndex = 1; rowIndex <= numRows; rowIndex++ ) {
	
	var comparison_value = form.comparison_amount.value;
	var myComparison = Math.round(comparison_value*10000)/10000;
	eval('user_value = form.doodle'+row+'.value;');
	var myUser = Math.round(user_value*10000)/10000;
		alert(myComparison);
		alert(myUser);
	}

Open in new window


The HTML is this:

<table style="width:650px; margin:auto;" border="1"><form name="dfm" action="javascript_loop.php" method="Post">
<tr>
<td>
<input type="text" size="35" name="doodle1" value="10000" onchange='show_visibility(dfm,"1","3")'>
</td>
</tr>
<tr>
<td>
<input type="text" size="35" name="doodle2" value="1500" onchange='show_visibility(dfm,"2","3")'>
</td>
</tr>
<tr>
<td>
<input type="text" size="35" name="doodle3" value="4500"onchange='show_visibility(dfm,"3","3")'>
</td>
</tr>
<tr>
<td style="text-align:center"><input type="hidden" id="allocatedlistcount" value="3">
<input type="hidden" id="comparison_amount" value="500">
<input type="submit" name="submit" value="submit"></form>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center; font-size:10px; color:#000000;"><br><br>Livin' the Dream...<br><br>
</td>
</tr>
</table>

Open in new window


This should be cake and ice cream, right?

So here are the values that I have in my table before I do the "onchange" event:

doodle1 = 125
doodle2 = 1256
doodle3 = 5687

According to my logic, I should see a total of six alerts, two for each of the three rows. The sequence should be:

alert #1: 500 (my comparison value)
alert #2: 125 (the doodle1 value)
alert #3: 500
alert #4 1256 (the doodle2 value)
alert #5: 500
alert #6 5687 (the doodle3 value)

But instead, I get this:

alert #1: 500 (my comparison value)
alert #2: 125 (the doodle1 value)
alert #3: 500
alert #4 125 (the doodle1 value)
alert #5: 500
alert #6 125 (the doodle1 value)

AHA! Do you smell that? I'm thinking that I've discovered what's flawed in that my loop isn't advancing to the next row, as far as my values. So how do I fix that?
ASKER CERTIFIED SOLUTION
Avatar of Sar1973
Sar1973
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it!

Thanks!