@Christonja/

Palindrome Checker

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7078182652845932","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"LypKYXZhU2NyaXB0IEFsZ29yaXRobXMgYW5kIERhdGEgU3RydWN0dXJlcyBQcm9qZWN0czogUGFsaW5kcm9tZSBDaGVja2VyOiAKQ2hhbGxlbmdlIGRlc2lnbmVkIGJ5IEZyZWVDb2RlQ2FtcCwgc29sdXRpb24gZGVyaXZlZCBjb21wbGV0ZWx5IGZyb20gbXkgbGVhcm5pbmcgb2J0YWluZWQgdGhhbmtzIHRvIEZyZWVDb2RlQ2FtcCBhbmQgb3RoZXIgc291cmNlcyBvbiB0aGUgaW50ZXJuZXQsIG5vdGFibHkgVzNTY2hvb2xzLmNvbSwgZnVuY3Rpb24gY2FsbHMgcHJvdmlkZWQgYnkgRnJlZUNvZGVDYW1wIGFzIHdheXMgdG8gdGVzdCB0aGUgYWxnb3JpdGhtKi8KCmZ1bmN0aW9uIHBhbGluZHJvbWUoc3RyKSB7CiAgLy8gR29vZCBsdWNrIQoKLy9DcmVhdGUgYSBuZXcgcmVnZXhwIGZvciBjaGVja2luZyBmb3IgYWxwaGFudW1lcmljIGNoYXJhY3RlcnMgaW4gc3RyaW5nCiAgdmFyIHJlZ2V4cCA9IC9bMC05YS16QS1aXSsvZzsKLy9TcGxpdCBldmVyeSBjaGFyYWN0ZXIgb2Ygc3RyaW5nIGludG8gaXQncyBvd24gYXJyYXkgZWxlbWVudAogIHZhciB0ZW1wQXJyID0gc3RyLnNwbGl0KCIiKTsKLy9GaWx0ZXIgdGhlIG5ldyBhcnJheSBiYXNlZCBvbiBpZiBpdCBtYXRjaGVzIHRoZSByZWdleHAgY3JlYXRlZCBlYXJsaWVyLCBpZiBpdCBkb2VzLCBwbGFjZSBpbnRvIHRoZSBuZXcgdmFyaWFibGUgc3RyQXJyCiAgdmFyIHN0ckFyciA9IHRlbXBB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3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*JavaScript Algorithms and Data Structures Projects: Palindrome Checker: 
Challenge designed by FreeCodeCamp, solution derived completely from my learning obtained thanks to FreeCodeCamp and other sources on the internet, notably W3Schools.com, function calls provided by FreeCodeCamp as ways to test the algorithm*/

function palindrome(str) {
  // Good luck!

//Create a new regexp for checking for alphanumeric characters in string
  var regexp = /[0-9a-zA-Z]+/g;
//Split every character of string into it's own array element
  var tempArr = str.split("");
//Filter the new array based on if it matches the regexp created earlier, if it does, place into the new variable strArr
  var strArr = tempArr.filter(arr => arr.match(regexp))
//Create a newStrArr and then push all strArr elements onto this newStrArr in reverse order
  var newStrArr = [];
  for (var i = strArr.length-1; i >= 0; i--) {
    newStrArr.push(strArr[i]);
  }
//Join up the newStrArr back into a string and conver to lower case
  var reverseStr = newStrArr.join("").toLowerCase();
//Create a finalstr from the original str that corresponds with the lowercase formatting and alphanumeric characters of the reversestr 
  var finalStr = str.split(/[^A-Za-z0-9]/).join("").toLowerCase();
//Log the reverse and final strings for purpose of debugging and as a visual console representation
  console.log("Reverse String: " + reverseStr + " | " + "Original String: " + finalStr);
//Perform the check if strings equal then the input string is indeed a palindrome, otherwise return false
  if (reverseStr === finalStr) {
    return true
  }
  return false;
}

palindrome("eye") //should return a boolean.
palindrome("eye") //should return true.
palindrome("_eye") //should return true.
palindrome("race car") //should return true.
palindrome("not a palindrome") //should return false.
palindrome("A man, a plan, a canal. Panama") //should return true.
palindrome("never odd or even") //should return true.
palindrome("nope") //should return false.
palindrome("almostomla") //should return false.
palindrome("My age is 0, 0 si ega ym.") //should return true.
palindrome("1 eye for of 1 eye.") //should return false.
palindrome("0_0 (: /-\ :) 0-0") //should return true.
palindrome("five|\_/|four") //should return false.
Native Browser JavaScript