JavaScript FreeCodeCamp Palindrome

时间:2017-10-05 19:36:59

标签: javascript arrays palindrome

我编写了以下代码来检查JS中的回文。我无法使用以下字符串:

palindrome("My age is 0, 0 si ega ym.")
palindrome("A man, a plan, a canal. Panama")

我不明白为什么。我知道有更简单的方法,但我想了解为什么这个特别不起作用。这是代码:

function palindrome(str) {
  //gets rid of all special characters, turns string to lowercase
  str = str.replace(/[^a-z0-9]/g, '');
  str = str.toLowerCase();
  //turns string into an array of characters, including whitespaces
  array = str.split("");
  //removes white space in the array
  array = array.filter(function(val) {
    return /\S/.test(val);
  });
  //defines new variable for the length of the array
  var length = array.length;
  //while loop to cycle through the array. Originally used zero, but I realized that wouldnt work for an odd-
  // numbered array. takes off the first and last value and compares them. If they are not equal, returns 
  // false. 
  while (length > 1) {
    var a = array.shift();
    var b = array.pop();
    length = array.length; //sets new length

    if (a != b) {
      return false;
    }
  }
  return true;
}

console.log(palindrome("My age is 0, 0 si ega ym."))
console.log(palindrome("A man, a plan, a canal. Panama"))

4 个答案:

答案 0 :(得分:1)

在小写字符串之前,你要删除大写字符。

例如:"一个男人,一个计划,一个运河。巴拿马"

str = str.replace(/[^a-z0-9]/g, '');

我们现在有:" manaplanacanalanama" - 因为你已经说过"杀死任何不是小写或数字的东西。糟糕。

将订单更改为此,它将起作用!

  str = str.toLowerCase();
  str = str.replace(/[^a-z0-9]/g, '');

答案 1 :(得分:1)

您的第一个正则表达式区分大小写,只匹配小写字母,因此您将删除所有大写字母。

通过添加一个字符来修复:

(或者,您可以在使用正则表达式之前移动.toLowerCase()行)

function palindrome(str) {
  //gets rid of all special characters, turns string to lowercase
  str = str.replace(/[^a-z0-9]/ig, '');
  str = str.toLowerCase();
  //turns string into an array of characters, including whitespaces
  array = str.split("");
  //removes white space in the array
  array = array.filter(function(val) {
    return /\S/.test(val);
  });
  //defines new variable for the length of the array
  var length = array.length;
  //while loop to cycle through the array. Originally used zero, but I realized that wouldnt work for an odd-
  // numbered array. takes off the first and last value and compares them. If they are not equal, returns 
  // false. 
  while (length > 1) {
    var a = array.shift();
    var b = array.pop();
    length = array.length; //sets new length

    if (a != b) {
      return false;
    }
  }
  return true;
}

console.log(palindrome("My age is 0, 0 si ega ym."));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome('Ada'));
console.log(palindrome('Dada'));

答案 2 :(得分:0)

  1. 在小写字符串之前,首先删除非低位字母数字字符。交换这两行,这样你首先将字符串转换为所有小写字符,然后删除非字母数字。
  2. 由于您已经删除了不是a-z0-9的字符,因此您根本不需要考虑空格!这意味着您可以删除filter(),在这种情况下不执行任何操作。
  3. function palindrome(str) {
      //gets rid of all special characters, turns string to lowercase
      str = str.toLowerCase();
      str = str.replace(/[^a-z0-9]/g, '');
    
      array = str.split("");
      //defines new variable for the length of the array
      var length = array.length;
      //while loop to cycle through the array. Originally used zero, but I realized that wouldnt work for an odd-
      // numbered array. takes off the first and last value and compares them. If they are not equal, returns 
      // false. 
      while (length > 1) {
        var a = array.shift();
        var b = array.pop();
        length = array.length; //sets new length
    
        if (a != b) {
          return false;
        }
      }
      return true;
    }
    
    console.log(palindrome("My age is 0, 0 si ega ym."))
    console.log(palindrome("A man, a plan, a canal. Panama"))

答案 3 :(得分:0)

将字符串移动到替换上方的小写,然后重试。

function palindrome(str) {
  //gets rid of all special characters, turns string to lowercase
    str = str.toLowerCase();
    str = str.replace(/[^a-z0-9]/g, '');
    //turns string into an array of characters, including whitespaces
    array = str.split("");
    //removes white space in the array
    array = array.filter(function(val) {
      return /\S/.test(val);
    });
    //defines new variable for the length of the array
    var length = array.length;
    //while loop to cycle through the array. Originally used zero, but I                         realized that wouldnt work for an odd-
    // numbered array. takes off the first and last value and compares them. If they are not equal, returns
    // false.
    while (length > 1) {
      var a = array.shift();
      var b = array.pop();
      length = array.length; //sets new length

      if (a != b) {
        return false;
      }
    }
    return true;
  }

  console.log(palindrome("My age is 0, 0 si ega ym."))
  console.log(palindrome("A man, a plan, a canal. Panama"))