Yeah I corrected the syntax error, but the two codes are apparently not identical as the second one is solving the problem which is the same as that asked in the question
Then you have an error somewhere else, because the two constructs are identical under the condition identified.