Merge pull request #7 from BillalPatel/palindrome

Improved function
This commit is contained in:
Billal Patel 2019-11-02 22:04:59 +00:00 committed by GitHub
commit cc52b90fd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -1,17 +1,17 @@
const palindromes = function(word) { const palindromes = function(word) {
const wordArr = word.replace(/\W/g, '').toLowerCase().split(''); const wordArr = word.replace(/\W/g, '').toLowerCase().split('');
const reversedArray = wordArr.reverse(); const reversedArr = wordArr.slice().reverse();
newArray = []; var status = false;
for (var i = 0; i < wordArr.length; i++) { for (var i = 0; i < wordArr.length; i++) {
if (wordArr[i] === reversedArray[i]) { if (wordArr[i] === reversedArr[i]) {
newArray.push(wordArr[i]) status = true;
} else { } else {
break; break;
} }
} }
return true ? newArray.join('') === word.replace(/\W/g, '').toLowerCase() : false; return status;
} }
module.exports = palindromes module.exports = palindromes;