How come my javascript input-function always return false? [duplicate]


Thanks for being able to help!

I want to know why my function always returns false, and how I can fix it.

I’ve created this in javascript to do different things depending on if the result of check is true or false.

else if (textNodes[15].id === 16 && nextTextNodeId === 16)  {
        showSongContainer();
        if(check) {
            showTextNode(17);
        }
        if (!check) {
            textElement.innerText = 'That’s superwrong! Maybe if you would use that small brain of yours you’d figure it out!';
            showTextNode(11);
        }

The above code runs this function first, to determine if a text input returns true (stairway to heaven) or false (anything other than stairway to heaven):

function showSongContainer() {
    songContainer.style.display = 'unset';
    const songInput = document.getElementById('songInput');
    const songButton = document.getElementById('songButton');

    songButton.addEventListener('click', (e) => {
        e.preventDefault();
        let value = songInput.value;
       
     check = ("stairway to heaven"===value || "Stairway to heaven"===value);
    });
} 

However, when the first else if starts (in the first codeblock), it always goes to the if (!check), before the text input has even been entered. Why is this? And how do I make it so that it returns the first if (in the first code block) if the text input is correct, and the other if, if incorrect?

I also have let check = ”; in a global scope in the begining of my code, if that has anything to do with it.

Im pretty new to coding, so please dont be too harsh 😀

Let me know if you need any additional information.

Cheers.

Source: JavaSript – Stack Overflow

November 19, 2021
Category : News
Tags: function | if-statement | input | javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.