 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 7th, 2011, 09:04 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
How to compare current value with previous value in a loop
Hi,
How do I compare the current value with previous value in a loop in javascript and determine if they are different?
The code below is my trial but to no avail. I am not comparing with the previous value, I am comparing only with the first value and this does not work for me. I must compare the values.
I would appreciate some help.
Cheers
Suppose I have a collection like this:
foo, foo, baz, foo
var firstName = names[0].value
for(var i =0; i < names.length; i++)
{
currentName = names[i].Value;
if(firstName != currentName)
{
textbox.disabled = true;
break;
}
}
|
|

July 7th, 2011, 09:07 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You need to update the previous value inside the loop as well. E.g.:
Code:
for(var i =0; i < names.length; i++)
{
currentName = names[i].Value;
if(firstName != currentName)
{
textbox.disabled = true;
break;
}
firstName = names[i].Value;
}
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 7th, 2011, 09:11 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
thanks for the reply. I will try that.
|
|

July 7th, 2011, 09:16 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
1 - But what happnes to firstName? You have not assigned it to any value? Do I still need to have my variable at the top ( var firstName = selectedNodes[0].value)
2 - Please could explain to me how this works?
for(var i =0; i < names.length; i++)
{
currentName = names[i].Value;
if(firstName != currentName)
{
textbox.disabled = true;
break;
}
firstName = names[i].Value;
}
Cheers
C
|
|

July 7th, 2011, 09:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Do I still need to have my variable at the top ( var firstName = selectedNodes[0].value)
|
You'll need to assign it an empty value. This way there is no previous value. By renaming firstName to previous value, things may become clearer:
Code:
var previousValue = '';
for(var i =0; i < names.length; i++)
{
currentName = names[i].Value;
if(previousValue != currentName) // Will be true on the first hit as previousValue == ''. Not sure if this is what you want
{
textbox.disabled = true;
break;
}
previousValue = names[i].Value; // Assign previousValue the current value. In the next iteration of the loop, this means the previous value
}
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 7th, 2011, 10:14 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
the previousValue cannot be an empty string otherwise it will be true and I will break out of the loop.
In my collection I have foo, foo,bar.foo. Therefore, if I compare the first value with the second then the condition is false because firts value == second value.
Then when I compare the second value with third value the condition will be true since second value != third value and I get out of the loop.
This is what I am trying to achive. If all the items in the collection are the same then nothing happens but if at least one is different then I have to detect it and get out.
|
|

July 7th, 2011, 10:37 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I think your first suggestion is going to work. I am comparing two values instead of one. What do you think?
Cheers
var selectedNodes = getNodeSet(xmlResult.xml, xmlResult.xml.documentElement,"//Insertion[@selected='true']/OrderNumber")
var firstSigName = selectedNodes[0].getAttribute("SignerName")
var firstIssueDate = getNode(xmlResult.xml, selectedNodes[0].parentNode, "Issue/@date")
for(var i =0; i < selectedNodes.length; i++)
{
var currentIssueDate = getNode(xmlResult.xml, selectedNodes[i].parentNode, "Issue/@date")
currSigName = selectedNodes[i].getAttribute("SignerName")
if(firstIssueDate.nodeValue != currentIssueDate.nodeValue && firstSigName != currSigName)
{
break;
}
firstSigName = selectedNodes[i].getAttribute("SignerName");
firstIssueDate = currentIssueDate;
}
|
|

July 7th, 2011, 05:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Many ways to Rome. If it works, it works ;-)
But if you're only looking for identical values, there's no need to keep track of the previous value. You can assign the first element to a variable, and then loop from the second element to the last, and compare them with the first. I think you had that in your first example, but you said that didn't work. However, this:
Quote:
|
If all the items in the collection are the same then nothing happens but if at least one is different then I have to detect it and get out.
|
seems to suggested this were what you're looking for....
This works for me and breaks out on the baz value:
Code:
var names = 'foo,foo,baz,foo'.split(',');
function Test(names)
{
var firstName = names[0];
for (var i = 0; i < names.length; i++)
{
currentName = names[i];
if (firstName != currentName)
{
textbox.disabled = true;
break;
}
}
}
Test(names);
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 8th, 2011, 05:13 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Imar,
Thanks for all your help
|
|
 |