Check for Palindromes - Reverse non-functional


For one I am not quite sure why dev.console is not discussed earlier in the lessons. Everyone needs to know ‘some sort of ‘debug system’’. But my ‘reverse’ code just doesn’t seem to be working. I am wondering ‘why not’, or what I am ‘missing’. I could of course just ‘brute force’ it into another array via a for/loop in reverse order, but that defeats the point of learning these ‘high level’ commands.

I think I have the ‘selection’ code worked out via one way or another, but if it doesn’t reverse I am ‘stuck’.

Your code so far

function palindrome(str) {
  // Good luck!
  lowerCaseStr = str.toLowerCase();
  lowerCaseStr = lowerCaseStr.replace(/\W/gi,'').replace(/_/g,'');
  var originalStr = lowerCaseStr.split('');
  var reverseStr = originalStr;

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

reverseStr actually gets reversed, but the gotcha is that reverseStr refers to the same array that originalStr refers to. Simply using var reverseStr = originalStr doesn’t copy the array in originalStr to reverseStr.

In the end, you’re printing the same (reversed) array twice, just with different names.

Ah, okay, it is only a ‘pointer’ than—

I should just add, I see better now with a pure assignment you are only setting the new pointer to the old one. I’ve since found .slice(0) which effectively does the trick. Maybe I missed mention of this somehow but it would be useful, I think, to talk about pointers and such properties, before getting to this section, as well as explaining the difference between copying/cloning an array/variable, and the accepted methods for doing so.

It is not technically a pointer as in C, but it is a reference.