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 6132443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:06:11+00:00 2026-05-23T17:06:11+00:00

I saw this script in another thread on stack overflow and tried to play

  • 0

I saw this script in another thread on stack overflow and tried to play around with it. Now to the problem, the script works fine in all browsers except internet explorer (I’m using version 7), here it only returns the first string (or the first row of that string if it is a larger one) but in all other browsers it returns what I want, in other words the longest common substring. And I was just wondering if there is some obvious error in this code below that some of you bright fellows out there can point out that Internet explorer doesn’t like, and if so how to improve it?
Thanks in advance =)

 function longestCommonSubstring(string1, string2){
            // init max value
            var longestCommonSubstring = 0;
            var theCommonString = '';

            // init 2D array with 0
            var table = Array(string1.length);
            for(a = 0; a <= string1.length; a++){
                    table[a] = Array(string2.length);
                    for(b = 0; b <= string2.length; b++){
                            table[a][b] = 0;
                    }
            }
            // fill table
            for(var i = 0; i < string1.length; i++){
                    for(var j = 0; j < string2.length; j++){
                            if(string1[i]==string2[j]){
                                    if(table[i][j] == 0){
                                            table[i+1][j+1] = 1;
                                    } else {
                                            table[i+1][j+1] = table[i][j] + 1;
                                    }
                                    if(table[i+1][j+1] > longestCommonSubstring){
                                            longestCommonSubstring = table[i+1][j+1];
                                            theCommonString = string1.substr(i + 1 - longestCommonSubstring, longestCommonSubstring);

                                    }
                            } else {
                                    table[i+1][j+1] = 0;
                            }
                    }
            }
            return theCommonString;
    }

EDIT___________________________________________________________

This is how my code now looks like after the improvments that has been suggested, but it still doesn’t work in internet explorer it just (in my case) returns the first row of the file.

function longestCommonSubstring(string1, string2){
        // init max value
        var longestCommonSubstring = 0;
        var theCommonString = '';
        var a;
        var b;
        // init 2D array with 0
        var table = [string1.length];
        for(a = 0; a <= string1.length; a++){
                table[a] = [string2.length];
                for(b = 0; b <= string2.length; b++){
                        table[a][b] = 0;
                }
        }
        // fill table
        for(var i = 0; i < string1.length; i++){
                for(var j = 0; j < string2.length; j++){
                        if(string1[i]===string2[j]){
                                if(table[i][j] === 0){
                                        table[i+1][j+1] = 1;
                                } else {
                                        table[i+1][j+1] = table[i][j] + 1;
                                }
                                if(table[i+1][j+1] > longestCommonSubstring){
                                        longestCommonSubstring = table[i+1][j+1];
                                        theCommonString = string1.substr(i + 1 - longestCommonSubstring, longestCommonSubstring);

                                }
                        } else {
                                table[i+1][j+1] = 0;
                        }
                }
        }
        return theCommonString;
}
  • 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. Editorial Team
    Editorial Team
    2026-05-23T17:06:12+00:00Added an answer on May 23, 2026 at 5:06 pm

    IE7 doesn’t let you access strings as arrays as you’re doing; you’ll need to use the substring property instead:

    if(string1.substring(i, i + 1)==string2.substring(j, j + 1)){
    

    in case you’re wondering, I figured it out by placing alert statements here and there: after the string comparison was successful, Firefox showed “a=a” but IE7 showed “undefined=undefined” :^\

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

Sidebar

Related Questions

I saw this in an answer to another question , in reference to shortcomings
I saw this the other day (scroll all the way down to see some
I saw this syntax: @helper IncludeJS(string url) { <script src=@url type=text/javascript></script> } Placed on
I tried to use this script pdfmeat to get data about papers from google
I just saw this in a script mysql_free_result($rawdb); I saw it on a script
I saw code in a project which looks like this: <script src='http://toolbar.wibiya.com/toolbarLoader.php?toolbarId=22800' type='text/javascript'></script> I
Saw this piece of code in a Ruby on Rails book. This first one
I saw this same question for VIM and it has been something that I
I saw this quote on the question: What is a good functional language on
I saw this question asked about C# I would like an answer for PHP.

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.