このスクリプトは、スタックオーバーフローした別のスレッドで見て、それを使って遊んだ。今問題では、スクリプトはInternet Explorer(私はバージョン7を使用しています)以外のすべてのブラウザで正常に動作します。ここでは最初の文字列(またはそれが大きい場合はその文字列の最初の行)ブラウザは、私が望むもの、つまり最長の共通部分文字列を返します。そして、私はちょうどそこにいくつかの明白な仲間が、インターネットエクスプローラが好きではないことを指摘することができ、もし改善すれば、このコードにいくつかの明らかな誤りがあるかどうか疑問に思っていましたか?事前に 感謝=)Internet Explorerでこのスクリプトが動作しないのはなぜですか? (v 7を使用)
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_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ __ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _
これは私のコードは、今どのように見えるかです提案されている改良の後でも、Internet Explorerではまだ動作しません。私の場合は、ファイルの最初の行が返されます。
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;
}
これはおそらく問題ではありませんが、最初のネストされたループの "a"と "b"が 'var'で宣言されていることを確認してください。配列を初期化するときには、 '' Array(something) 'ではなく' [] 'を使うだけです。 – Pointy
@Pointyいいえ、とにかくありがたいです) – erik
あなたが使用していることを確認してください!==と===これはタイプ強制をプリフォームしません。 –