String Variable Changing In an if else Statement, why am I getting warnings?

String Variable Changing In an if else Statement, why am I getting warnings?
0

#1

This ‘works’, but I am being told that my else statement’s variables are out of scope or that the variables have already been defined. Is this a large problem or a small problem.

function palindrome(str) {
  str.toLowerCase();
  var x = str.replace(/\W+/g, "");
  var length = x.length;
  var middle = (length/2);
   if (length % 2 !== 0){
     var a = (x.substring(0, middle));
     var b =  (x.substring(middle + 1));
  } else {
       a  = (x.substring(0, middle));
       b  = (x.substring(middle));
  }

#2

Because they are declared in the if part of the statement. Better to declare them before if statement.


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.