Hello,
I just completed the Palindrome checker project successfully, my code passes all the test.
My code:
function palindrome(str) {
//Remove all non-alphanumeric characters and turn the resulting array into a lower cased string
let regex = /[A-Za-z0-9]/gi;
let arr = str.match(regex).join("").toLowerCase();
//Create two arrays, one original one reversed
let reversedArr = arr.split("").reverse();
let originalStringArr = arr.split("");
//console.log(arr);
//Check for palindromes
let count = 0;
for (let i = 0; i < originalStringArr.length; i++) {
if (reversedArr[i] === originalStringArr[i]) {
count++;
//console.log(count);
}
}
if (originalStringArr.length === count) {
return true;
} else {
return false;
}
}
palindrome("2A3*3a2");
After passing the tests, I checked the official solutions and obviously noticed that the code could much simpler and more brief. If my code passes the tests, but is not as good as the official solution am I supposed to refactor my code, to make it more in line with the solution?
I think that the main reason why my code is longer is, because I find it easier to first split the code apart into a couple of steps, write that down on paper and then write the actual code.
So is the goal of the projects to write code that would be in line with the official solution or just write any code that you are capable of coming up with on your own, that passes the tests?