Solved

What am I doing wrong with this Javascript function?

Posted on 2014-03-05
17
193 Views
Last Modified: 2014-03-06
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?
0
Comment
Question by:brucegust
  • 9
  • 8
17 Comments
 
LVL 9

Expert Comment

by:Sar1973
ID: 39908934
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.
0
 

Author Comment

by:brucegust
ID: 39909403
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?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909414
At line 13: replace var_ticker=var_ticker+1; with var_ticker++;
0
 

Author Comment

by:brucegust
ID: 39909435
Now my alert reads "0" every time.

I'm still blowing it somewhere...
0
 

Author Comment

by:brucegust
ID: 39909443
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

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909490
It just means that the condition you have set stops after 3 steps; what should be wrong with this...?
0
 

Author Comment

by:brucegust
ID: 39909501
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.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909529
Can you tell me how many rows you have...? If you have 3 alerts, what should be wrong?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:brucegust
ID: 39909626
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.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909647
So 3 is the total number of rows...? And why do you round to 10.000 your result?
0
 

Author Comment

by:brucegust
ID: 39909684
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?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909695
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.
0
 

Author Comment

by:brucegust
ID: 39909781
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.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39909804
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
0
 

Author Comment

by:brucegust
ID: 39909835
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?
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 500 total points
ID: 39909854
I'm sorry for the delay, I think I've got it: you get fixed at step 1 because at line 7 and 8 of your original code (in the eval function) you use a "row" variable instead of the index "rowindex" of the iteration. Please refresh also the other lines in which you use row (like 'view_comparison_'+row+'\).
0
 

Author Comment

by:brucegust
ID: 39909886
That did it!

Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now