Issue with if/else block

I am working through the Palindrome checker and having an issue where the if/else block in my code is hitting the else statement when it shouldn’t have. This only seems to occur when I pass in a text value that has a space in it. If I only pass in values without a space it works as expected.

Here is my code so far.

const checkButton = document.getElementById("check-btn");
const inputValue = document.getElementById('text-input');

checkButton.addEventListener('click', event => {
    if(inputValue.value === 'input text here'){
        alert("please Enter Text!!")
    } else {
        if(inputValue.value.includes(' ')){
        let str = checkForSpaces(inputValue.value)
            checkForPalindrom(str);
        }
        checkForPalindrom(inputValue.value);
    }
});

const checkForPalindrom = (input) => {
    const textArray = input.split('');
    const revArray = [];
    while(textArray.length > 0){
        let endString = textArray.pop();
        revArray.push(endString);
    }
    let newWord =  revArray.join('');
    if(input === newWord){
        alert('YOU HAVE A PALENDROME!!!')
    }else{ 
        alert('You dont have a Palendrome!!')
    }
}

const checkForSpaces = (input) => {
    const spaceArray = input.split('');
    let indexOfFirst = input.indexOf(' ');
    spaceArray.splice(indexOfFirst,1);
    let newSTR = spaceArray.join('');
   return newSTR;
} 

What I thought should happen was when you pass a value that has a space the checkForSpaces function would get executed first. This would remove the space and return a string without space. Which then would pass the new string value to my checkForPalindrom function with out the space where it reverses the string and checks if its the same as original value. There for it should only fall into the else block if the value you have is not a palindrome. However it seems to be executing both the if block and then the else block for values that contained a space.

I’m lost.

Executing both code in the if and else sounds strange. Which if/else is that specifically?

Wouldn’t it be easier if you set up your code so it outputs to the correct element and check the input properly. That way, you can focus on the actual tests that are failing because of issues with the logic.

Note: You’ll need to remove all non-alphanumeric characters

I would also suggest logging out input and newWord and trying an input such as race car

Thanks for the tip @lasjorg . I had to refactor the even listener code to the below and things started functioning as expected.

removed

Please don’t post solution code.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

I completely understand where you are coming from. However I would argue that what I posted in no way solves the overall challenge for the Palindrome project. As what I have was done in my own IDE on my machine and not the website. Also I have in not way answered or implemented all the user stories you all have posted for the project. So if someone were to take my code they would be no closer to solve the project than anyone else. As they would have to back trace the logic to understand how it works so they could finish out and implement the user stories. What I posted just simply shows how I fixed my issue.

Also there are some people who learn best when they are truly stuck by looking at the answer and then working backwards from that. I myself can be one of those people.

Jake

The rule is no posting answers no matter how much you like the idea of doing so for whatever reasons :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.