odin-default-js-exercises/09_palindromes/palindromes.js

44 lines
974 B
JavaScript
Raw Normal View History

2022-09-06 12:18:39 +00:00
const palindromes = function (val) {
/*
let separatedBy = [" ", "!", ".", ","];
let separated;
let reversed;
let joined = val;
let removed
2017-08-25 19:07:28 +00:00
2022-09-06 12:18:39 +00:00
for (i = 0; i < separatedBy.length; i++) {
let temp = joined.split(separatedBy[i]);
joined = temp.join("");
}
removed = joined.split("");
for (i = removed.length - 1; i >= 0; i--) {
reversed.push(separated[i]);
}
if (separated === reversed) {
return true;
} else {
return false;
}
*/
let separatedBy = [" ", "!", ".", ","];
let separated = val.toLowerCase().split("");
let reversed = [];
for (i = 0; i < separatedBy.length; i++) {
separated = separated.filter(function (d) {
return d != separatedBy[i];
});
}
for (i = separated.length - 1; i >= 0; i--) {
reversed.push(separated[i]);
}
if (separated.join("") === reversed.join("")) {
return true;
} else {
return false;
}
};
2017-08-25 19:07:28 +00:00
// Do not edit below this line
module.exports = palindromes;