迹忆客 专注技术分享

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

JavaScript 中的Anagram查找器

作者:迹忆客 最近更新:2023/06/04 浏览次数:

在本文中,我们将学习 JavaScript 中 anagram 的概念。 我们将学习如何检查一个字符串是否是 JavaScript 中另一个字符串的变位词。


Anagram 的概念

具有相同字符和相同数字的两个单词或字符串称为变位词。 单词的长度也应该相同。

使用变位词,我们可以重新排列一个单词的字符以生成另一个单词。 以下是一些字谜词示例:

  1. listen and silent
  2. triangle and integral
  3. the eyes and they see

正如我们所知,我们可以重新排列一个单词的字符并借助算法转换第二个单词。


JavaScript 中的Anagram查找器

在 JavaScript 中,我们可以创建一个程序来检查两个单词或字符串是否是彼此的变位词。 我们可以创建一个函数来检查单词是否是变位词并将布尔结果返回为 true 或 false。

首先,我们应该对两个字符串进行排序并比较排序后的字符串,以确定它们是否相同。

以下示例将使用 JavaScript 程序对两个单词执行变位词检查算法。 我们将使用默认的 JavaScript 方法 split()sort()join() 来检查提供的单词。

split() 方法用于拆分字符串字符并生成字符数组。 sort() 方法用于对元素数组进行排序。

join() 方法用于连接元素数组并生成一串元素。

function checkAnagram(word1, word2) {
   let length1 = word1.length;
   let length2 = word2.length;
   if(length1 !== length2){
       console.log(word1+" and "+word2+" lengths did not match!");
      return
   }
   let wordA = word1.split('').sort().join('');
   let wordB = word2.split('').sort().join('');

   if(wordA === wordB){
      console.log(word1+" and "+word2+" are anagram to each other");
   } else {
      console.log(word1+" and "+word2+" are not anagram to each other");
   }
}

let word1 = "silent";
let word2 = "listen";

checkAnagram(word1,word2)

word1 = "shouts"

checkAnagram(word1,word2)

输出:

"silent and listen are anagram to each other"
"shouts and listen are not anagram to each other"

如上所示,我们声明了一个 let 类型的函数 checkAnagram() ,我们将在其中将单词作为参数。 在该函数内部,我们对传递的单词使用了条件语句。

使用条件语句,我们首先检查了两个单词的长度是否相等。 如果不是,我们会在日志中显示长度不匹配消息。

然后,我们对传递的单词使用 split()sort()join() 对单词进行排序,并将排序后的单词存储在单独的变量 word1 和 word2 中。

然后,我们需要使用条件语句 if-else 检查两个变量的值是否相等。 如果相等,则这些词是一个字谜; 如果不是,则这些词不是字谜。

我们已经初始化了两个单词,silent 和 listen,并将它们作为参数传递给 checkAnagram() 函数,更新了 word1,并再次调用 checkAnagram()

console.log() 方法在日志中显示结果。 您可以复制并保存上述源代码,然后使用 JavaScript 编译器查看结果。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便