简体   繁体   中英

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

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. There for, your while loop never end because of undefined === undefined.

Returning same sized array to handle nested palis.

ex: abxyxZxyxab => 00030703000 odd numbered nested palis.

ex: asddsa => 003000 even numbered pali.

ex: asdttqwe => 00020000 i dont know if this is a pali but here we go

smallest pali is 2 char wide so i start at index:1 and increment till 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. 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 .

Here we use array reverse() method to match palindromes.

Also I added additional minLength parameter and duplication removal logic to make it more nice.

 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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