D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
cch5ng
Full window
Github gist
// source http://jsbin.com/nurixetuzi
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> var test1 = 'ana'; var test2 = 'abba'; var test3 = 'abbz'; var test4 = 'annad'; var test5 = 'I'; var test6 = ''; var test7 = 'max exam'; var test8 = 'aNna'; var test9 = 'Avid diva'; //helper functions var reverse = function(word) { var arr = []; for (var i = 0; i < word.length; i++) { arr[i] = word[word.length - 1 - i]; arr[word.length - 1 - i] = word[i]; } return arr.join(''); }; var removeSpaces = function(word) { var spaceRemoved = []; for (var j = 0; j < word.length; j ++) { if (word[j] != ' ') { spaceRemoved.push(word[j]); } } return spaceRemoved.join(''); }; var isPalindrome = function(phrase) { var cleanPhrase = removeSpaces(phrase).toLowerCase(); //console.log(cleanPhrase); var result = false; if (cleanPhrase.length === 0) { console.log('Please enter a word or phrase'); } else if (cleanPhrase.length === 1) { result = true; } else if (cleanPhrase.length % 2 === 0) { //console.log(cleanPhrase.slice(0, phrase.length / 2)); //console.log(cleanPhrase.slice(phrase.length / 2)); //console.log(reverse(cleanPhrase.slice(phrase.length / 2))); if (cleanPhrase.slice(0, cleanPhrase.length / 2) == reverse(cleanPhrase.slice(cleanPhrase.length / 2))) { result = true; } } else { //odd number of characters //console.log(cleanPhrase.slice(0, Math.floor(cleanPhrase.length / 2))); //console.log(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2))); //console.log(reverse(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2)))); if (cleanPhrase.slice(0, Math.floor(cleanPhrase.length / 2)) == reverse(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2)))) { result = true; } } if (result) { console.log(phrase + ' is a palindrome'); } else { console.log(phrase + ' is not a palindrome'); } }; //testing helper function //console.log(removeSpaces(' abc jk')); //console.log(removeSpaces('ab cjk ')); //main tests isPalindrome(test1); isPalindrome(test2); isPalindrome(test3); isPalindrome(test4); //edge cases isPalindrome(test5); isPalindrome(test6); //testing input with spaces isPalindrome(test7); //testing input with upper case and lower case chars isPalindrome(test8); isPalindrome(test9); /* resources used https://www.palindromelist.net/ */ </script> <script id="jsbin-source-javascript" type="text/javascript">var test1 = 'ana'; var test2 = 'abba'; var test3 = 'abbz'; var test4 = 'annad'; var test5 = 'I'; var test6 = ''; var test7 = 'max exam'; var test8 = 'aNna'; var test9 = 'Avid diva'; //helper functions var reverse = function(word) { var arr = []; for (var i = 0; i < word.length; i++) { arr[i] = word[word.length - 1 - i]; arr[word.length - 1 - i] = word[i]; } return arr.join(''); }; var removeSpaces = function(word) { var spaceRemoved = []; for (var j = 0; j < word.length; j ++) { if (word[j] != ' ') { spaceRemoved.push(word[j]); } } return spaceRemoved.join(''); }; var isPalindrome = function(phrase) { var cleanPhrase = removeSpaces(phrase).toLowerCase(); //console.log(cleanPhrase); var result = false; if (cleanPhrase.length === 0) { console.log('Please enter a word or phrase'); } else if (cleanPhrase.length === 1) { result = true; } else if (cleanPhrase.length % 2 === 0) { //console.log(cleanPhrase.slice(0, phrase.length / 2)); //console.log(cleanPhrase.slice(phrase.length / 2)); //console.log(reverse(cleanPhrase.slice(phrase.length / 2))); if (cleanPhrase.slice(0, cleanPhrase.length / 2) == reverse(cleanPhrase.slice(cleanPhrase.length / 2))) { result = true; } } else { //odd number of characters //console.log(cleanPhrase.slice(0, Math.floor(cleanPhrase.length / 2))); //console.log(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2))); //console.log(reverse(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2)))); if (cleanPhrase.slice(0, Math.floor(cleanPhrase.length / 2)) == reverse(cleanPhrase.slice(Math.ceil(cleanPhrase.length / 2)))) { result = true; } } if (result) { console.log(phrase + ' is a palindrome'); } else { console.log(phrase + ' is not a palindrome'); } }; //testing helper function //console.log(removeSpaces(' abc jk')); //console.log(removeSpaces('ab cjk ')); //main tests isPalindrome(test1); isPalindrome(test2); isPalindrome(test3); isPalindrome(test4); //edge cases isPalindrome(test5); isPalindrome(test6); //testing input with spaces isPalindrome(test7); //testing input with upper case and lower case chars isPalindrome(test8); isPalindrome(test9); /* resources used https://www.palindromelist.net/ */</script></body> </html>