$("#palindromeButton").click(function () {
let palInput = $("#palindromeInput").val();
let revInput = palInput.split('').reverse().join('');
if (palInput.length < 2) {
Swal.fire({
title: 'Error!',
text: 'Please enter 2 or more letters',
icon: 'warning',
confirmButtonText: 'Okay',
})
} else {
let word = palInput.toLowerCase().replace(/[\W_]/g, '');
let revWord = word.split('').reverse().join('');
let results = ""
if (revWord === word && palInput === revInput) {
results = `This means ${palInput} is a palindrome!`
} else {
results = `This means ${palInput} is not a palindrome!`
}
if (palInput.match(/^[\W_a-zA-Z]/g)) {
$("#palindromeResult").text(`You input ${palInput}, which reversed is ${revInput}. ${results} `)
} else {
$("#palindromeResult").text(`You input the word ${palInput}, which reversed is ${revInput}. ${results} `)
}
}
})
我知道我可以简单地将其设置为仅word == revword,因此结果将等于true,但仍将显示revInput的反斜杠。我有一个回文挑战,它也接受非文字字符。除了使用“ \”和“ /”字符外,它几乎可以完美运行。在下面的代码中,当我运行任何组合回文......>
\m
意味着注意JS,因此它会立即进行评估。