Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function findPalindromes(str) that extracts from a given text all palindromes,
- e.g. "ABBA", "lamal", "exe". Write JS program palindromesExtract.js that invokes your function
- with the sample input data below and prints the output at the console. */
- "use strict";
- function findPalindromes(str) {
- var arr = str.split(/\W+/).filter(Boolean);
- var resultArr = [];
- for (var i = 0; i < arr.length; i += 1) {
- if (arr[i].length == 1) {
- resultArr.push(arr[i]);
- } else {
- var word = arr[i].toLowerCase();
- var reverseWord = '';
- for (var j = word.length - 1; j >= 0; j -= 1) {
- reverseWord += word[j];
- }
- if (reverseWord == word) resultArr.push(word);
- }
- }
- return resultArr.join(', ');
- }
- console.log(findPalindromes('There is a man, his name was Bob.'));
- console.log(findPalindromes('Javaj sos mam dad'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement