Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

46
Views
Compairing two Arrays if they are equal is returning false even if they are the same

With the following code I'm checking if str is a palindrome. (A palindrome is a word or sentence that's spelled the same way both forward and backward). Even tho they are the same, the function is still giving me a false return... Why is that? I know I could join the arrays to single strings with .join('') but I still would like to know whats wrong with my code and why it's not working

function palindrome(str) {
    
      let newStr = str.toUpperCase().replace(/[^A-Z\d]/g, '').split('');
      let newStrRev = [...newStr].reverse();     //.reverse() is not destructive anymore
    
      console.log(typeof newStr, newStr);        //OUTPUT: object [ 'R', 'A', 'C', 'E', 'C', 'A', 'R' ]
      console.log(typeof newStrRev, newStrRev);  //OUTPUT: object [ 'R', 'A', 'C', 'E', 'C', 'A', 'R' ]
    
      if (newStr === newStrRev) {
        return true
      } else {
        return false
      }
    }
    
    console.log(palindrome("race car"));        //OUTPUT: false
5 months ago ยท Santiago Trujillo
Answer question
Find remote jobs