From 1dce6620191d591491b1028760356aae559a1498 Mon Sep 17 00:00:00 2001 From: Adelina Moroaca Date: Sat, 23 Dec 2023 16:12:05 +0200 Subject: [PATCH] Add palindrome solution --- 09_palindromes/palindromes.js | 17 ++++++++++++++--- 09_palindromes/palindromes.spec.js | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/09_palindromes/palindromes.js b/09_palindromes/palindromes.js index 8d21018..3d0f4c1 100644 --- a/09_palindromes/palindromes.js +++ b/09_palindromes/palindromes.js @@ -1,6 +1,17 @@ -const palindromes = function () { - +const palindromes = function(str) { + let strL = str.toString().toLowerCase().toUpperCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/[\W_]+/g, ""); + let newStr = ''; + for(i = strL.length; i >= 0; i--){ + newStr += strL.charAt(i); + // console.log(newStr) + } + if (strL === newStr) { + return true; + } else { + return false; + } }; - +palindromes('lid off a daffodil') // true +palindromes('tacos') // false // Do not edit below this line module.exports = palindromes; diff --git a/09_palindromes/palindromes.spec.js b/09_palindromes/palindromes.spec.js index 90d53e4..d019a7f 100644 --- a/09_palindromes/palindromes.spec.js +++ b/09_palindromes/palindromes.spec.js @@ -4,25 +4,30 @@ describe('palindromes', () => { test('works with single words', () => { expect(palindromes('racecar')).toBe(true); }); - test.skip('works with punctuation ', () => { + test('works with punctuation ', () => { expect(palindromes('racecar!')).toBe(true); }); - test.skip('works with upper-case letters ', () => { + test('works with upper-case letters ', () => { expect(palindromes('Racecar!')).toBe(true); }); - test.skip('works with multiple words', () => { + + //de facut + test('works with multiple words', () => { expect(palindromes('A car, a man, a maraca.')).toBe(true); }); - test.skip('works with multiple words', () => { + + //de facut + test('works with multiple words', () => { expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true); }); - test.skip('doesn\'t just always return true', () => { + + test('doesn\'t just always return true', () => { expect(palindromes('ZZZZ car, a man, a maracaz.')).toBe(false); }); - test.skip('works with numbers in a string', () => { + test('works with numbers in a string', () => { expect(palindromes('rac3e3car')).toBe(true); }); - test.skip('works with unevenly spaced numbers in a string', () => { + test('works with unevenly spaced numbers in a string', () => { expect(palindromes('r3ace3car')).toBe(false); }); });