简体   繁体   English

浏览器在循环JavaScript算法时崩溃

[英]Browser crashing while loop JavaScript algorithm

Guys i'm trying to write an algorithm where I pass in a large string and let it loop through the string and whatever palindrome it finds, it pushes into array but for some reason my browser is crashing once i put in the while loop and I have no 伙计们,我正在尝试编写一种算法,在该算法中,我传入一个大字符串,然后让它循环遍历字符串,无论找到什么回文,它都会推送到数组中,但是由于某种原因,一旦我进入while循环,我的浏览器就会崩溃,我没有

function arrOfPalindromes(str) {
  var palindromeArrays = []
  var plength = palindromeArrays.length

  // grab first character
  // put that in a temp
  // continue and look for match
  // when match found go up one from temp and down one from index of loop
  // if matched continue
  // else escape and carry on
  // if palendrome push into array

  var counter = 0;
  for (var i = 0; i < str.length; i++) {

    for (var j = 1; j < str.length - 1; j++) {
      if (str[i + counter] === str[j - counter]) {
        while (str[i + counter] === str[j - counter]) {
          console.log(str[j], str[i])
            // append the letter to the last index of the array
          palindromeArrays[plength] += str[i]
          counter++
        }
      }
    }
  }

  return palindromeArrays
}

var result2 = arrOfPalindromes('asdfmadamasdfbigccbigsdf')
console.log(result2)

Do not mention about the algorithm but the condition 不提算法,但条件

while (str[i + counter] === str[j - counter])

Make your code crash. 使您的代码崩溃。 A little surprise but str[j+counter] when j+counter > str.length return undefined and the same as j-counter <0. 当j + counter> str.length返回未定义且与j-counter <0相同时,str [j + counter]有点令人惊讶。 There for, your while loop never end because of undefined === undefined. 在那里,由于未定义===未定义,因此while循环永远不会结束。

Returning same sized array to handle nested palis. 返回相同大小的数组以处理嵌套的palis。

ex: abxyxZxyxab => 00030703000 odd numbered nested palis. 例如:abxyxZxyxab => 00030703000奇数编号的嵌套palis。

ex: asddsa => 003000 even numbered pali. 例如:asddsa => 003000偶数为pali。

ex: asdttqwe => 00020000 i dont know if this is a pali but here we go 例如:asdttqwe => 00020000我不知道这是否是巴利语,但在这里我们开始

smallest pali is 2 char wide so i start at index:1 and increment till str.len-1 最小的pali宽度为2个字符,因此我从index:1开始并递增直到str.len-1

for (var i = 1; i < str.length-1; i++) {
    counter=0;
    while(str[i]+1-counter == str[i]+counter || str[i]-counter == str[i]+counter) { // always true when counter is 0
    // while (even numbered palis || odd numbered palis)
    // IF counter is bigger than 0 but we are still here we have found a pali & middle of the pali is i(or i+0.5) &size of the pali is counter*2(or+1)

        if(str[i]+1-counter == str[i]+counter){//even sized pali
            res[i]=counter*2;
        }else{//odd sized pali
            res[i]=counter*2+1;
        }
    counter++;//see if its a bigger pali.
    }
}

not super optimized while + if,else checks same stuff. 没有超级优化,而+ if,else检查相同的东西。 These can be somehow merged. 这些可以以某种方式合并。 Maybe even even and odd can be handled without any checks. 甚至偶数和奇数都可以通过任何检查来处理。

You don't need to use three loops. 您不需要使用三个循环。 You can do it with two for loops where one starts from the beginning and other one is from the end of the string . 您可以使用两个for循环来完成此操作,其中一个从string的开头开始,另一个从string

Here we use array reverse() method to match palindromes. 在这里,我们使用array reverse()方法来匹配回文。

Also I added additional minLength parameter and duplication removal logic to make it more nice. 此外,我还添加了其他minLength参数和重复删除逻辑,以使其更加美观。

 function findPalindromes(str, minLength) { var palindromes = []; var _strLength = str.length; for (var i = 0; i < _strLength; i++) { for (var j = _strLength - 1; j >= 0; j--) { if (str[i] == str[j]) { var word = str.substring(i, j + 1); //Check if the word is a palindrome if (word === word.split("").reverse().join("")) { //Add minimum length validation and remove duplicates if(word.length >= minLength && palindromes.indexOf(word) === -1){ palindromes.push(word); } } } } } return palindromes; } var result = findPalindromes('asdfmadamasdfbigccbigsdf', 2) console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM