PhiAgent
PhiAgent

Reputation: 158

why is the js equality operator giving me different answers?

Even though newArray.join("") and x.toUpperCase() evaluate to the same string 'MADAM', newArray.join("") === x.toUpperCase() doesn't return true.

Can someone help me understand why that's the case.

Disclaimer: Function is not designed for one-letter words.

function isPalindrome(x) {
  a=x.toUpperCase().split("")
  if (a.length===1) {
    return a
  }else {
    newArray=[a.pop()]
    newArray=newArray.concat(isPalindrome(a.join("")))
  }
  return newArray.join("")
}

console.log('newArray.join(\'\'):', isPalindrome("Madam"));

function isPalindrome(x) {
  a=x.toUpperCase().split("")
  if (a.length===1) {
    return a
  }else {
    newArray=[a.pop()]
    newArray=newArray.concat(isPalindrome(a.join("")))
  }
  return x.toUpperCase()
}

console.log('x.toUpperCase():', isPalindrome("Madam"));

However:

function isPalindrome(x) {
  a=x.toUpperCase().split("")
  if (a.length===1) {
    return a
  }else {
    newArray=[a.pop()]
    newArray=newArray.concat(isPalindrome(a.join("")))
  }
  return (newArray.join("") === x.toUpperCase())
}

console.log('===', isPalindrome("Madam"));

I already tested the strict equality expression and found it to work in the example below.

x="madam"
a=x.toUpperCase().split("")

console.log(a.join("")===x.toUpperCase());//logs true

Upvotes: 0

Views: 79

Answers (2)

PhiAgent
PhiAgent

Reputation: 158

There's no flaw in the strict equality comparison operator. Below is a better and more robust solution that handles single-letter words too.

function isPalindrome(string) {

  //first remove possibility of difference by different cases
  string=string.toUpperCase()

  //there are some exceptions that need to be taken care of
  //Exceptions
  //single letter words
  if (string.length===1) {
    return true
  }
  //two letter words
  if (string.length===2) {
    if (string[0]===string[1]) {
      return true
    }else{
      return false
    }
  }
  //after taking care of exceptions, you can use recursion to complete
  if (string.length>2) {
    if (string[0]!==string.slice(-1)) {
      return false
    }else{
      output=isPalindrome(string.slice(1,-1))
      return output
    }
  }
  return false
}

isPalindrome("maDAm")//evaluates to true
isPalindrome("a")//evaluates to true
isPalindrome("aba")//evaluates to true
isPalindrome("as")//evaluates to false

Upvotes: 0

yqlim
yqlim

Reputation: 7080

The comparison operator is not flawed. It's something in your code.

Look at this line:

newArray = newArray.concat(isPalindrome(a.join("")));

When your function returns the string in either newArray.join('') case or x.toUpperCase() case, they return the same values. So far, so good.

But when your function returns newArray.join('') === x.toUpperCase(), it becomes a boolean.

Meaning, in the highlighted line above, it will not ever produce the word 'MADAM'.

See below example for illustration. It is your function, I've only added console.log to let you know what values are returned:

function isPalindrome(x) {
  a=x.toUpperCase().split("")
  if (a.length===1) {
    return a
  }else {
    newArray=[a.pop()]
    newArray=newArray.concat(isPalindrome(a.join("")))
  }
  
  console.log('Raw:', newArray);
  console.log('Joined:', newArray.join(''));
  console.log('Comparison:', newArray.join(''), '===', x.toUpperCase());
  
  return (newArray.join("") === x.toUpperCase())
}

isPalindrome('Madam')

Conclusion

Your current function will not work. You have to use another way to complete this task.

PS

Since you are asking about the potential comparison operator flaw, so I am only answering that.

It is not flawed.

Upvotes: 1

Related Questions