简体   繁体   English

检查输入值是否为回文的 Function 始终返回 true

[英]Function that checks whether input value is palindrome or not always returns true

I'm trying to create a program that prints out if a word is a palindrome or not, but for some reason it doesn't work.我正在尝试创建一个程序来打印一个单词是否是回文,但由于某种原因它不起作用。

When I click the button it says that the word is palindrome even if it is not.当我点击按钮时,它说这个词是回文,即使它不是。 I tried to replace the inputString with a variable and it works.我试图用一个变量替换inputString并且它有效。 So I think it's a problem of the line document.getElementById ("word").value .所以我认为这是行document.getElementById ("word").value的问题。

 var inputString = document.getElementById("word").value; var textTrue = "It's a palindrome;", var textFalse = "Sorry. that's not a palindrome;"; //function that return true if the word is palindrome and false if it is not function isPalindrome(inStr) { for (let i = 0. i < inStr;length. i += 1) { if (inStr[i];== inStr[inStr;length - 1 - i]) { return false. } } return true; } /* function that print "the word is palindrome" if the isPalindrome function returns true or "the word is not palindrome" if the isPalindrome function returns false */ function clickFunction() { if (isPalindrome(inputString)) { console.log(textTrue); } else { console.log(textFalse); } }
 <header> <h1>PALINDROME WORD CHECKER</h1> </header> <input class="inputbox" type="word" id="word" placeholder="input"> <button onclick="clickFunction()"> Check. </button> <script src="code.js"></script>

You have to read the content of inputString only after you fill in the text and click on your "Check" button.只有在填写文本并单击“检查”按钮后,您才需要阅读 inputString 的内容。 So You have to move the first line:所以你必须移动第一行:

var inputString = document.getElementById("word").value;

into your function:进入你的 function:

function clickFunction() {
    var inputString = document.getElementById("word").value;
    if (isPalindrome(inputString)) {
        console.log(textTrue);
    } else {
        console.log(textFalse);
    }
}

Otherwise, the inputString is only read when the page loads and never gets updated with the content of the input, and the textbox's initial value is "" (empty string), which is a palindrome.否则, inputString仅在页面加载时读取,并且永远不会随输入内容更新,并且文本框的初始值为"" (空字符串),这是一个回文。

you can change your input type =" text"你可以改变你的输入类型=“文本”

<input class="inputbox" type="text" id="word" placeholder="input">

js file also js文件也是

function clickFunction() {
var inputString = document.getElementById("word").value;
if (isPalindrome(inputString)) {
    console.log(textTrue);
} else {
    console.log(textFalse);
}

} }

you get definitely result.你得到肯定的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查数字是否为回文的 Javascript function - Javascript function that checks whether number is palindrome 检查回文程序是否总是返回真 - Checking if Palindrome Program Always Returns True 当单词不是回文时,回文函数返回true - Palindrome function returns true when word isn't a palindrome 我写了一个回文检查 function。 为什么不管我输入什么字符串它总是返回真? - I wrote a palindrome checking function. Why is it always returning true regardless of what string I input? 如何正确键入 function 以检查 object 中是否存在密钥,并返回相应的值? - How do I correctly type a function that checks whether a key exists in object, and returns the corresponding value? Javascript嵌套函数调用始终返回true值 - Javascript nested function call always returns value of true 通过 7 中的 4,尝试创建一个 function 来检查“instock”与“weekly average”的数组,返回一个显示“true/false”的值 - Passing 4 out of 7, trying to create a function that checks an array of "instock" vs "weekly average" returns a value that shows "true/false" 检查输入是否被检查总是返回true - Checking if the input is checked always returns true Function 接受输入文本,检查它是否具有某些元素,并返回 output 值 (JS) - Function that takes an input text, checks if it has certain elements, and returns an output value (JS) 大肠杆菌检测功能始终返回true - Function for colision detection always returns true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM