Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 139441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:26:08+00:00 2026-05-11T07:26:08+00:00

I have trouble dealing with my for loops now, I’m trying to compare two

  • 0

I have trouble dealing with my for loops now, I’m trying to compare two datum, basically it will compare 2 items, then it will write the matches and the mismatches on the webpage.

I managed to write the matches on the webpage, it was working good. But there’s a bug in my mismatch compare.

It wrote all the data on the webpage X times, here’s my JS code:

function testItems(i1, i2) {     var newArray = [];     var newArray2 = [];     var count = 0;     var count2 = 0;     for(var i = 0; i < i1.length; i++) {         for(var j = 0; j < i2.length; j++) {             if(i1[i] == i2[j]) {                 newArray.push(i1[i]);                 count++;             } if (i1[i] !== i2[j]) {                 newArray2.push(i1[i]);                 count2++;             }         }     }     count-=2;     count2-=2     writeHTML(count,count2, newArray, newArray2); } 

The result was horrible for the mismatches:

alt text http://www.picamatic.com/show/2009/03/01/07/44/2523028_672x48.jpg

I was expecting it to show the mistakes, not all the strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:26:08+00:00Added an answer on May 11, 2026 at 7:26 am

    The issue you’re seeing is because of the nested for loop. You are essentially doing a cross-compare: for every item in i1, you are comparing it to every item in i2 (remember that j starts again at 0 every time i advances… the two loops don’t run in parallel).

    Since I understand from the comments below that you want to be able to compare one array to the other, even if the items in each are in a different order, I’ve edited my original suggestion. Note that the snippet below does not normalize differences in case between the two arrays… don’t know if that’s a concern. Also note that it only compares i1 against i2… not both i1 to i2 and i2 to i1, which would make the task a little more challenging.

    function testItems(i1, i2) {      var newArray = [];     var newArray2 = [];      for (var i = 0; i < i1.length; i++) {         var found = false;         for (var j = 0; j < i2.length; j++) {             if (i1[i] == i2[j]) found = true;         }         if (found) {             newArray.push(i1[i])         } else {             newArray2.push(i1[i])         }     } } 

    As an alternative, you could consider using a hash table to index i1/i2, but since the example of strings in your comment include spaces and I don’t know if you’re using any javascript helper libraries, it’s probably best to stick with the nested for loops. The snippet also makes no attempt to weed out duplicates.

    Another optimization you might consider is that your newArray and newArray2 arrays contain their own length property, so you don’t need to pass the count to your HTML writer. When the writer receives the arrays, it can ask each one for the .length property to know how large each one is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If all requests to site B are mapped to an… May 11, 2026 at 2:15 pm
  • added an answer It turns out the same DTC security configuration window on… May 11, 2026 at 2:15 pm
  • added an answer Since you're new, it probably makes sense not to drill… May 11, 2026 at 2:15 pm

Related Questions

I have trouble comparing 2 double in Excel VBA suppose that I have the
I have trouble with the following piece of code. When I go through with
I have trouble using Perl grep() with a string that may contain chars that
I have trouble defining a trigger for a MySQL database. I want to change
I have no trouble building 1.35.0, as well as 1.36.0 on the timesys arm-gcc
I have trouble dealing with my for loops now, I'm trying to compare two
I have a file that contains this kind of paths: C:\bad\foo.c C:\good\foo.c C:\good\bar\foo.c C:\good\bar\[variable
I have been using the ASP.NET AJAX UpdatePanel control a lot lately for some
I have an object that can build itself from an XML string, and write

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.