My code is working fine but when the if...else statement detected the 'x' letter, it do not returns false




Hey, @Grasshopper_Ben. My code is working fine, however, when the if statement detected the ‘x’ letter, it do not return false. What is wrong with this code. I rechecked a lots of times. Please answer.

This code will never output “false” because “letter” will never be “x” as you do not have “x” in the text array. In the code above, letter is equal to “export”. Yes, there is the letter “x” in it but your code is checking if “letter” is equal to “x” not if the letter “x” is detected.

Another thing, after you return something in a function, the function is finished. So the console.log you wrote after the “return true” will never get executed.

Lmao, how to fix it?

  • Pummarin

I dunno, but I think you can use .contains() method

What about those console.log() I writen below those return statements?

I’ve one one solution:

Just apply it to your code… The return statement will not prevent the “console.log(5)” to execute.

Here there is no “3” logged to the console because the “return false” has stopped the function. So the console.log is never executed.

Hey there, your function isn’t checking if a word contains the letter 'x'. It’s checking if a word is equal to 'x'. Try using the .includes() method to check if a string contains a specific character.

Also, when the function hits a return statement, it immediately stops running and returns a value. Any code placed below a return statement will never run. Try moving your console.log() statements up a line so they are before the return statements.

Hope this helps!
Ben

1 Like

Thanks, Ben! I did’t noticed what codes did I do wrong! I moved console.log() statements up before those return statements and I used .includes method to make sure that the function found the letter ‘x’!

P :grinning: