Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2011, 09:04 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default 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;
}
}
Reply With Quote
  #2 (permalink)  
Old July 7th, 2011, 09:07 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
pallone (July 7th, 2011)
  #3 (permalink)  
Old July 7th, 2011, 09:11 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Imar,

thanks for the reply. I will try that.
Reply With Quote
  #4 (permalink)  
Old July 7th, 2011, 09:16 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old July 7th, 2011, 09:55 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
pallone (July 7th, 2011)
  #6 (permalink)  
Old July 7th, 2011, 10:14 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #7 (permalink)  
Old July 7th, 2011, 10:37 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

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;
}
Reply With Quote
  #8 (permalink)  
Old July 7th, 2011, 05:56 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
pallone (July 8th, 2011)
  #9 (permalink)  
Old July 8th, 2011, 05:13 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for all your help
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare sql database field to name of the current page SouthendSupporter ASP.NET 3.5 Basics 2 July 12th, 2009 07:39 AM
Compare current value with previous parent node ratjetoes XSLT 2 July 17th, 2008 06:15 AM
Accessing previous item in current-group() in loop mikeymikey XSLT 1 January 4th, 2008 06:47 AM
Compare previous value of DropDownList on Postback densom ASP.NET 2.0 Basics 0 October 23rd, 2006 02:10 PM
how to check the current value and the previous va tllcll BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 October 24th, 2005 02:25 PM



All times are GMT -4. The time now is 07:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.