Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac7c79f

Browse files
committed
Palindromes
1 parent 73d88ec commit ac7c79f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Once inside, run the command below:
1111
2. Vowels counter
1212
3. Most recurring character
1313
4. Sentence Capitalization
14+
5. Palindromes

src/palindromes/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// pick a solution and insert here to run the test.
2+
3+
4+
function palindromeChecker(text) {
5+
var textLen = text.length;
6+
for (var i = 0; i < textLen / 2; i++) {
7+
if (text[i] !== text[textLen - 1 - i]) {
8+
return false;
9+
}
10+
}
11+
return true;
12+
}
13+
14+
module.exports = palindromeChecker;

src/palindromes/solutions.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// AN INTUITIVE APPROACH
2+
3+
function palindromeChecker(text) {
4+
5+
var reversedText = text.toLowerCase()
6+
.split('').reverse().join('')
7+
8+
return text === reversedText
9+
}
10+
11+
12+
// LOOPING THROUGH AND COMPARING CHARACTERS
13+
14+
15+
function palindromeChecker(text) {
16+
let charArray = text.toLowerCase().split('')
17+
18+
let result = charArray.every((letter, index) => {
19+
return letter === charArray[charArray.length - index - 1];
20+
})
21+
22+
return result
23+
}
24+
25+
// LOOPING THROUGH AND COMPARING CHARACTERS(OPTIMIZED)
26+
27+
28+
function palindromeChecker(text) {
29+
var textLen = text.length;
30+
for (var i = 0; i < textLen / 2; i++) {
31+
if (text[i] !== text[textLen - 1 - i]) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}

src/palindromes/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const palindromeChecker = require('./index');
2+
3+
test('palindromeChecker is a function', () => {
4+
expect(typeof palindromeChecker).toEqual('function');
5+
});
6+
7+
test('"php" is a palindrome', () => {
8+
expect(palindromeChecker('php')).toBeTruthy();
9+
});
10+
11+
test('" php " is not a palindrome', () => {
12+
expect(palindromeChecker(' php ')).toBeFalsy();
13+
});
14+
15+
test('"developer" is not a palindrome', () => {
16+
expect(palindromeChecker('developer')).toBeFalsy();
17+
});
18+
19+
test('"2002" a palindrome', () => {
20+
expect(palindromeChecker('2002')).toBeTruthy();
21+
});
22+

0 commit comments

Comments
 (0)