Skip to content

Commit 6ba50d6

Browse files
committed
[added]: Palindrome Checker Challenge
1 parent fd7d0d8 commit 6ba50d6

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Make sure to star the repository if you find it useful. And contributions to the
1111
Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz". For example, your program should print:
1212

1313
```javascript
14-
1
14+
1
1515
2
1616
Fizz
1717
4
@@ -29,3 +29,7 @@ FizzBuzz
2929
16
3030
...
3131
```
32+
33+
## Challenge 2: Palindrome Checker
34+
35+
Create a function that takes a string as an argument and returns `true` if it's a palindrome and `false` if it's not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. For example, "racecar" is a palindrome, but "hello" is not.

solutions/ch_1_FizzBuzz/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz". For example, your program should print:
44

55
```javascript
6-
1
6+
1
77
2
88
Fizz
99
4
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function isPalindrome(str) {
2+
// Convert the string to lowercase and remove any non-alphanumeric characters
3+
const cleanStr = str.toLowerCase().replace(/[^a-z0-9]/g, "");
4+
5+
// Loop through the first half of the string and compare with the corresponding character in the second half
6+
for (let i = 0; i < Math.floor(cleanStr.length / 2); i++) {
7+
if (cleanStr[i] !== cleanStr[cleanStr.length - 1 - i]) {
8+
// If the characters don't match, the string is not a palindrome
9+
return false;
10+
}
11+
}
12+
13+
// If we've made it through the loop, the string is a palindrome
14+
return true;
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Challenge 2: Palindrome Checker
2+
3+
Create a function that takes a string as an argument and returns `true` if it's a palindrome and `false` if it's not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. For example, "racecar" is a palindrome, but "hello" is not.
4+
5+
## Answer Explanation
6+
7+
The function takes a single argument, str, which is the string to check for palindromicity. Here's what the function does:
8+
9+
- First, it converts the string to lowercase using the toLowerCase method. This ensures that the function will be case-insensitive when checking for palindromicity.
10+
- Next, it removes any non-alphanumeric characters from the string using a regular expression. This step is optional, but it ensures that the function will only consider the alphanumeric characters when checking for palindromicity.
11+
- The function then loops through the first half of the cleaned string and compares each character with the corresponding character in the second half of the string. The loop uses Math.floor(cleanStr.length / 2) to determine how many iterations are needed, since we only need to compare the first half of the string with the second half.
12+
- If the characters at corresponding positions in the first and second halves of the string don't match, the function immediately returns false, indicating that the string is not a palindrome.
13+
- If the function makes it through the loop without finding any non-matching characters, it returns true, indicating that the string is a palindrome.
14+
15+
Here's an example usage of the function:
16+
17+
```javascript
18+
console.log(isPalindrome("racecar")); // true
19+
console.log(isPalindrome("hello")); // false
20+
console.log(isPalindrome("A man, a plan, a canal, Panama!")); // true
21+
```
22+
23+
In this example, the function correctly identifies that "racecar" and "A man, a plan, a canal, Panama!" are palindromes, but "hello" is not.

0 commit comments

Comments
 (0)