迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中的回文

作者:迹忆客 最近更新:2024/03/21 浏览次数:

回文是一个单词、数字、短语或其他可以前后阅读的字符序列,例如 adidaracecar。还有数字回文,包括使用短数字的日期和时间戳。

例如,2011 年 11 月 11 日 被认为是回文日 111111,使用 dd-mm-yy 格式,因为它可以从左到右阅读,反之亦然。句子长度回文忽略标点符号、大写字母和单词边界。

这篇文章教我们检查字符串/数字是否是 JavaScript 中的回文。


在 JavaScript 中使用内置函数进行回文运算

JavaScript 提供了几个函数,我们可以使用它们来检测字符串是否为回文。

  1. 首先,使用 string.split() 函数将字符串转换为数组。

split() 方法是 JavaScript 提供的内置方法。此方法将字符串拆分为子字符串列表。

通过将分隔符作为输入来执行此切割。

这个分隔符纯粹是一个可选的输入。并基于此,将所有这些子字符串放入一个数组中,并返回该数组。

你还可以将 limit 作为输入传递给该方法,从而限制数组中子字符串的数量。

  1. 其次,使用 reverse() 方法反转数组的值。

reverse() 方法是 JavaScript 提供的内置方法。此方法改为反转原始数组。

数组的第一个元素成为数组的最后一个元素,数组的最后一个元素成为第一个元素。

  1. 最后,使用 join() 方法将数组的值分组到字符串中。

join() 方法是 JavaScript 提供的内置方法。此方法通过连接数组(或类似数组的对象)中的所有元素创建并返回一个新字符串,由逗号或指定的分隔符字符串分隔。

如果数组只有一个元素,则返回该元素而不使用分隔符。

让我们结合上述所有方法并检查输入字符串是否为回文。

function palindromeFn(inputString) {
  return inputString == inputString.split('').reverse().join('');
}
console.log(palindromeFn('aviddiva'))
console.log(palindromeFn('Hello'))

如果我们在任何浏览器中运行上述代码,它将显示以下结果。

输出:

"true"
"false"

带有 for 循环的 JavaScript 回文

检查字符串是否为回文的另一种方法是使用 for 循环。以下是在 JavaScript 中检查字符串是否为回文的步骤。

  • 首先,取一个包含要检查的字符串长度的临时变量。
  • 应用 for 循环并设置条件以检查字符串的一半长度。
  • 将原始字符串字符与反转字符串字符进行比较。
  • 如果前后字符相同,则字符串或数字为回文。否则,指定的字符串或数字不是回文。
function palindromeFn(string) {
  const stringLength = string.length;
  for (let i = 0; i < stringLength / 2; i++) {
    if (string[i] !== string[stringLength - 1 - i]) {
      return 'It is not a palindrome.';
    }
  }
  return 'It is a palindrome.';
}
console.log(palindromeFn('aviddiva'));
console.log(palindromeFn('Hello'));

如果我们在任何浏览器中运行上述代码,它将显示以下结果。

输出:

"It is a palindrome."
"It is not a palindrome."

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便