简体   繁体   中英

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.

    $('#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.

Watch out, if you want to be case insensitive, you'll have to add ".toLowerCase()" on each side of the "===" test.

 $( 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; this return str === reversed; you don't want to have that there.

also, remember to invoke the pal()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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