palindrome - 我的 HTML 和 JS 回文脚本有什么问题或缺少什么?
问题描述
函数 isPalindrome (userEntry) { str = str.toLowerCase; str = str.replace(/[^az]/g, ""); str = (i = 0 || i 你的话不是回文"; } } document.getElementById("输出").innerHTML = "你的话是回文
"; } <h2>Palindrome detection</h2>
<code>Detect if a string is a palindrome</code><br /><br />
Enter a word with 10 or less characters <input type="text" id="userEntry"><br />
<button type="button" onclick="isPalindrome();">Enter</button><br /><br />
</body>
解决方案
我更改了 isPalindrome 中的一些代码并添加 div 以给出结果。
function isPalindrome (userEntry) {
var re = /[\W_]/g;
var lowRegStr = userEntry.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
if( reverseStr === lowRegStr){
document.getElementById("output").innerHTML = "Your word is a palindrome";
}
}
<h2>Palindrome detection</h2>
<code>Detect if a string is a palindrome</code><br /><br />
Enter a word with 10 or less characters <input type="text" id="userEntry"><br />
<button type="button" onclick="isPalindrome(document.getElementById('userEntry').value)">Enter</button><br /><br />
<div id="output"></div>
推荐阅读
- linux - 什么是韦兰的“损坏请求”?
- angular - Angular 反应式表单,基于另一个表单上的 FormControl 验证一个表单
- angular - 使用 NgbModal 服务时如何测试模态内容
- python-3.x - 如何在 Python 中打印排序列表中的前 5 个项目
- webpack-4 - 如何通过 webpack 4 中的 mini-css-extract-plugin 正确处理 css 文件?
- php - 简单的正则表达式匹配在线调试器,但在 PHP 7.3 运行时不匹配
- javascript - 不能在复选框上输入 addEventListener?
- regex - 用于在文件中的每一行(不包含句号)的末尾添加句号的 Shell 脚本
- javascript - 如何循环具有多个选择的表单,以生成多个表单?
- node.js - 尝试将文件加载到服务器时无法 POST /upload。穆尔特