Head-First-JavaScript-Programming
Head-First-JavaScript-Programming copied to clipboard
Exercise forwards and backwards.
Hi,
I am doing the final exercise for matching words forwards and backwards.
Curious about this line
var len = this.length-1;
When I try to do lexical scope as I walk through this exercise I am stumped as to how this variable gets the length. When I read this it seems that it is getting the length minus one character or this is getting the reversed order. Am I reading this correctly?
// Exercise forwards and backwards.
String.prototype.palindrome = function () {
// first we get the length of the string
var len = this.length-1;
// then we iterate over each character in the string, and test to see if the character at i is the the same
// as the character at len-i (ex: the character at the other end)
for (var i = 0; i <= len; i++) {
if (this.charAt(i) != this.charAt(len-i)) {
// if they are not equal we return false because we don't have a palindrom
return false;
}
if (i === (len-i)) {
// if we get to where i is in the middle of the string, or we get to the end of the loop, we return
// true because we've got a palindrome
return true;
}
}
// if we get to where i is in the middle of the string, or we get to the end of the loop, we return
// true because we've got a palindrome
return true;
}
Hi Zach, Remember that "this" in the function palindrome is the string you're calling the palindrome method on. So if you use this method like so:
"test".palindrome()
you're calling the method palindrome() on the string "test", so this is set to "test".
So when you write
var len = this.length - 1;
you're getting the length of "test" (4) subtracting 1 and getting 3 (which isn't actually the length, it's the index of the last character in the string).
Then in the for loop, we're using i <= len. We could have also done i < this.length there. Same thing (notice the <= in the first one and the < in the second). But because len is set to 1 less than the length it makes the charAt computation a bit easier (e.g. we can write this.charAt(len-i) instead of this.charAt(this.length-1-i).
Hope that makes sense!
Hi Beth,
Thanks so much for your reply. That part makes sense we are grabbing the last character of the string.
When we use the for loop how is it iterating through each character in the len variable if we are only grabbing the last character in the string? When we compare the charAt(i) != this.charAt(len-i) is len-i doing the oppostite? is it now grabbing the first character? but how does it iterate through all the characters if we are only iterating through the len variable which is only getting the last character?
// then we iterate over each character in the string, and test to see if the character at i is the the same
// as the character at len-i (ex: the character at the other end)
for (var i = 0; i <= len; i++) {
if (this.charAt(i) != this.charAt(len-i)) {
// if they are not equal we return false because we don't have a palindrom
return false;
}
if (i === (len-i)) {
// if we get to where i is in the middle of the string, or we get to the end of the loop, we return
// true because we've got a palindrome
return true;
}
}
// if we get to where i is in the middle of the string, or we get to the end of the loop, we return
// true because we've got a palindrome
return true;
}
Hi Zach, Well, remember i is increasing. So when i = 0, then you're getting the last character in the string; when i is 1 you're getting the 2nd to last, and so on. So each time through the loop you're comparing characters from the beginning of the string to characters from the end of the string. Work through the loop on paper a couple of times and make sure you follow.
Beth
Hi Beth,
I finally got it. Took my time with this exercise and worked through the loop on paper to have it finally set in.
Thank you again for taking the time to answer questions.
Cheers!