简体   繁体   English

回文代码看起来不错,但我无法让它运行

[英]Palindrome code looks fine but I can't get it to run

I am doing a palindrome coder challenge and I am very new to coding.我正在做一个回文编码器挑战,我对编码很陌生。 I cannot get it to run right to save my life.我无法让它正常运行以挽救我的生命。 I had someone look at it and they said it should be fine but obviously, something isn't right.我让某人看了一下,他们说应该没问题,但很明显,有些地方不对劲。 I have tried various small changes, updating in VS each time and nothing seems to work.我尝试了各种小改动,每次都在 VS 中更新,但似乎没有任何效果。

    $('#bntCrunch').on("click", function () {

        var str = $('input').val()
        function pal() {
            const reversed = str
                .split('')
                .reverse()
                .join('');

            return str === reversed;

            if (str == reversed) {
                document.getElementById('output').innerHTML(str);
            }

            else {
                document.getElementById('output').innerHTML("That is not a palindrome");
            }
        }
    })

</script>

VS seems to say "pal has been defined but its value is never read" no matter where I call the function.无论我在哪里调用 function,VS 似乎都在说“pal 已被定义,但它的值从未被读取”。

Watch out, if you want to be case insensitive, you'll have to add ".toLowerCase()" on each side of the "===" test.请注意,如果您想不区分大小写,则必须在“===”测试的每一侧添加“.toLowerCase()”。

 $( document ).ready(function() { $( "#button" ).on( "click", function() { $('#result').text($('#test').val()===$('#test').val().split('').reverse().join('')?"OK":"KO"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="test" /> <input type="button" id="button" value="Test" /> <div id="result"></div>

you are already returning a boolean value in pal() function;您已经在 pal() function 中返回了 boolean 值; this return str === reversed;这个返回 str === 反转; you don't want to have that there.你不想在那里。

also, remember to invoke the pal()另外,记得调用 pal()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM