1

I have to make a function that tests if a certain value when reversed is equal to it's original form. I get a positive integer a and this is the code I came up with

   var i = 0;
   var numlist1 = [];
   numlist1 = a.toString(10).replace(/\D/g, '0').split('').map(Number);
   var numlist2 = [];
   numlist2 = numlist1.reverse();
   var final=1;
   while (i<numlist1.length) {
      if (numlist1[i] != numlist2[i]) {
         final=0;
         break;
      }
      i=i+1;
   }
   var answer="yes";
   if (final == 0) {
      answer="no";
   }

however when I try using it the answer gets returned as yes, even when I input a number like 211 which is not equal to its reverse.

4 Answers4

1

The problem is that .reverse reverses the original array, so when you run numlist2 = numlist1.reverse(); you are reversing numlist1 and assigning a reference to it to numlist2. Instead you want to make a new identical array and reverse it:

function checkNum(a) {
  var i = 0;
  var numlist1 = [];
  numlist1 = a.toString(10).replace(/\D/g, '0').split('').map(Number);
  var numlist2 = [];
  numlist2 = numlist1.slice();
  numlist2.reverse();
  var final=1;
  while (i<numlist1.length) {
    if (numlist1[i] != numlist2[i]) {
       final=0;
       break;
    }
    i=i+1;
  }
  var answer="yes";
  if (final == 0) {
    answer="no";
  }
  return answer;
}

console.log(checkNum(212));
console.log(checkNum(2112));
console.log(checkNum(211));
console.log(checkNum(312));

Note that the .slice method of duplicating the array works for arrays of primitives only. If you were using ES6+ syntax I would have opted for the spread syntax (...) instead. Also you may want to run your code through a linter at some point-- you're playing a bit of a dangerous game around loose equality and arguably around where you are instantiating your vars, which could lead do difficult to debug issues at some point. Good luck!

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
1

You don't need to change the number to string if it's just a number.

There a simple logic for checking the number if its palindrome or not

function checkreverse(number) {
  var rem, temp, final = 0;
  temp = number;
  while (number > 0) {
    rem = number % 10;
    number = parseInt(number / 10);
    final = final * 10 + rem;
  }
  return final === temp;
}

console.log(checkreverse(211));
console.log(checkreverse(212));
console.log(checkreverse(21134));
console.log(checkreverse(211343112));
saurabh
  • 2,553
  • 2
  • 21
  • 28
0

The explanation is the same as what @Alexander Nied said, but with different implementation:

just change this line only:

numlist2 = [...numlist1].reverse(); 

EDIT:

You may also simplify the whole code like:

  var numlist1 = a.toString(10).replace(/\D/g, '0').split('').map(Number);
  var numlist2 = [...numlist1].reverse(); 
  var answer = (numlist1.join() === numlist2.join()) ? 'yes' : 'no';
Addis
  • 2,480
  • 2
  • 13
  • 21
0

You can also solve this problem using the method for finding string is a palindrome or not. Here, you need to convert a number to a string first and then check string is a palindrome (reverse of a string is equal to original string ) or not.

Please try with below solution.

function isRevEqNum(num) {
   let str = String(num);
   const iter = [...str.toLowerCase()];
   for (let char of iter) {
      if (char !== iter.pop()) { return 'no' }
   }
   return 'yes'
}
Prashant Biradar
  • 301
  • 5
  • 14