Thanks for taking the survey!

If you missed it, click here to access the anonymous survey.

Introduction

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.

Download the Lab

To get started, first download the ZIP file for Lab 6 here.

Completing the Lab

Part 1: Cleaning up the String

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:

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!

Part 2: Checking for 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:

  1. Create a for-loop to go through each character in the string.
  2. For each ith character, check if the ith character is the same as the ith character from the end; if it is not, it is not a palindrome and you should return false immediately.
  3. If all of your checks in your for-loop were successful, you have never returned 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

Test your code now!

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.

Part 3: Visualizing Data (Extra Credit)

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:

As 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:

  1. Create an array, arr, that is initially set to an empty array.
  2. Create a for-loop to go through the numbers 1 to 10000. To do this, you will use a for-loop that starts at 1 instead of 0 and goes until 10000 instead of something.length. Otherwise the syntax of the for-loop is the same as usual.
  3. Use the statement var s = i.toString(); to convert i (a Number) into a String.
  4. Use your 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.
  5. Once your loop completes, return the array.

Submission

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!