Mar. 3 - Mar. 5
A palindrome is a word, number, or phrase that can be read the same backwards and forwards. Some classical palindromes include "race car", "taco cat", and "Amor Roma" which are read the same backwards and forwards (ignoring both spaces and cases).
In this lab, you will develop a palindrome checker. For extra credit, you will use this palindrome checker to look at the distance between palindromes on the number line.
To get started, first download the ZIP file for Lab 6 here.
In lab6.js, we have provided an empty isPalindrome
function that takes in one parameter,
str
, that should be tested to determine if it is a palindrome. If str
is a palindrome,
the function must return true
; otherwise, it must return false
.
In order to test if a string is a palindrome, we will first clean up the string by making the entire string lowercase and by removing spaces. JavaScript provides some built-in functions to help us with that:
str = str.toLowerCase();
will make the string lowercase. You should put this statement
as the first line of code inside of your function.
str = str.replace(/\s+/g, "");
will remove all spaces from your string. You
should put this statement as the second line of code inside of your function.
At this point, your string has been cleaned so that the spaces are removed and the letters are now all lowercase. This means
that str
is ready to be checked if it is a palindrome!
In order to check if str
is a palindrome, you must check if the first character is the same as the last character,
then check if the second character is the same as the second to last character, then check if the third character is the same
as the third to last character, and so forth.
As a reminder, you can get a single character from a string by using either array brackets ([ ]
) or the charAt
method of a String. This means that both of these will get the character at index 3:
var c1 = str.charAt(3); var c2 = str[3];
Therefore, to check for a palindrome, you must:
false
immediately.
false
.
Therefore, the string must be a palindrome and you should return true
.
Step 2 in the previous list can be challenging. This table may help you understand how you need to complete Step 2:
Example value of str |
i | ith character | ith character from the end | Equal? |
---|---|---|---|---|
"racecar" |
0 |
[i] == [0] == "r" |
[str.length - 1 - i] == [7 - 1 - 0] == [6] == "r" |
Yes |
1 |
[i] == [1] == "a" |
[str.length - 1 - i] == [7 - 1 - 1] == [5] == "a" |
Yes | |
2 |
[i] == [2] == "c" |
[str.length - 1 - i] == [7 - 1 - 2] == [5] == "c" |
Yes | |
3 |
[i] == [3] == "e" |
[str.length - 1 - i] == [7 - 1 - 3] == [4] == "e" |
Yes | |
4 |
[i] == [4] == "c" |
[str.length - 1 - i] == [7 - 1 - 4] == [2] == "c" |
Yes | |
5 |
[i] == [5] == "a" |
[str.length - 1 - i] == [7 - 1 - 5] == [1] == "a" |
Yes | |
6 |
[i] == [6] == "r" |
[str.length - 1 - i] == [7 - 1 - 6] == [0] == "r" |
Yes | |
All equal, therefore return true
|
||||
"cs105" |
0 |
[i] == [0] == "c" |
[str.length - 1 - i] == [5 - 1 - 0] == [4] == "5" |
No |
"c" != "5" , therefore return false immediately
|
On your lab6.html web page, use the Palindrome Checker to test if your function works! Make sure to try both words that are palindromes and words that are not palindromes.
For +1 points, complete the generatePalindromeData
function that is part of your lab6.js.
This function must return an Array of Objects where each Object represents a number in the range [1, 10000)
that is a palindrome. Specifically, the Object must have two properties:
number
, the number that is a palindrome (as a Number)distanceFromPrevious
, the distance away from the previous number that was a palindromeAs an example, the following would be the return value for the numbers [1, 25]:
[ { number: 1, distanceFromPrevious: 0 }, /* distanceFromPrevious: 1 would also be acceptable */ { number: 2, distanceFromPrevious: 1 }, { number: 3, distanceFromPrevious: 1 }, { number: 4, distanceFromPrevious: 1 }, { number: 5, distanceFromPrevious: 1 }, { number: 6, distanceFromPrevious: 1 }, { number: 7, distanceFromPrevious: 1 }, { number: 8, distanceFromPrevious: 1 }, { number: 9, distanceFromPrevious: 1 }, /* 10 is not included because "10" is not a palindrome */ { number: 11, distanceFromPrevious: 2 }, /* 12..21 are not included because they are not a palindromes */ { number: 22, distanceFromPrevious: 11 } ]
To complete this, the general outline of your algorithm would be as follows:
arr
, that is initially set to an empty array.
1
instead of 0
and
goes until 10000
instead of something.length
. Otherwise the syntax
of the for-loop is the same as usual.
var s = i.toString();
to convert i
(a Number) into a String.
isPalindrome
function to check if s
is a palindrome. If it is
a palindrome, create an object with both number
and distanceFromPrevious
.
Use the push
method on the array arr
to add the object to the array.
To submit your lab, you must submit your lab6.js to the CS 105 website. You should submit ONLY .js file. You do not need to submit the other files in the folder. Click here to submit the lab!