From d76c55010b49aaae4f2f2c7d87cb431acb1bff70 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Tue, 12 Dec 2017 16:08:50 +0530 Subject: [PATCH 1/3] Create Check_for_palindrome.md --- snippets/Check_for_palindrome.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 snippets/Check_for_palindrome.md diff --git a/snippets/Check_for_palindrome.md b/snippets/Check_for_palindrome.md new file mode 100644 index 000000000..cc5a9267d --- /dev/null +++ b/snippets/Check_for_palindrome.md @@ -0,0 +1,10 @@ +### Check For Palindrome + +``` +function palindrome(str) { + var rg =/[\W_]/g; + var low_rep=str.toLowerCase().replace(rg,''); + var final= low_rep.split('').reverse().join(''); + return final == low_rep; + } + ``` From 17cef1d18a4fed5c701120491aad4be2b217af02 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Tue, 12 Dec 2017 16:42:59 +0530 Subject: [PATCH 2/3] Update Check_for_palindrome.md Yes it's working and I double checked --- snippets/Check_for_palindrome.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/Check_for_palindrome.md b/snippets/Check_for_palindrome.md index cc5a9267d..0dc98859f 100644 --- a/snippets/Check_for_palindrome.md +++ b/snippets/Check_for_palindrome.md @@ -1,10 +1,9 @@ ### Check For Palindrome +Steps : +1. First Converted to form in which no non-alphanumeric character is present i.e. string which is to be compared +2. Then Converted to palindrome form in which characters will be reversed and will be compared to non-alphanumeric string + ``` -function palindrome(str) { - var rg =/[\W_]/g; - var low_rep=str.toLowerCase().replace(rg,''); - var final= low_rep.split('').reverse().join(''); - return final == low_rep; - } +palindrome = str => (str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('')==str.toLowerCase().replace(/[\W_]/g,'')); ``` From 64928edec79b0c0e74b507afabe43ce128478385 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 16:22:50 +0200 Subject: [PATCH 3/3] Update Check_for_palindrome.md --- snippets/Check_for_palindrome.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/Check_for_palindrome.md b/snippets/Check_for_palindrome.md index 0dc98859f..4abd6ad1d 100644 --- a/snippets/Check_for_palindrome.md +++ b/snippets/Check_for_palindrome.md @@ -1,8 +1,7 @@ -### Check For Palindrome +### Check for palindrome -Steps : -1. First Converted to form in which no non-alphanumeric character is present i.e. string which is to be compared -2. Then Converted to palindrome form in which characters will be reversed and will be compared to non-alphanumeric string +Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric characters from it. +Then, `split('')` into individual characters, `reverse()`, `join('')` and compare to the original, unreversed string, after converting it `tolowerCase()`. ``` palindrome = str => (str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('')==str.toLowerCase().replace(/[\W_]/g,''));