Conversation
| function Palindromo(s) {} | ||
| function Palindromo(s) { | ||
| const reversed = s.split("").reverse().join("") | ||
| if (reversed == s) { |
There was a problem hiding this comment.
Great! Just a little warning, in JavaScript we have two operators for comparison: == and ===. While the first tries to convert both arguments to the same type (for example: '5' == 5 would be true), the last strictly compares both items, meaning they have to have the same value (for example: '5' === 5 would be false, but 5 === 5 would be true). Usually, we should always use === to avoid problems, unless we explicitly want to compare values not strictly.
In this case it would work either way, but since we know we are just comparing strings, there's no need to use ==.
On a final note, we could make this if ... else ... into just one line, like so: return reversed === s ;)
There was a problem hiding this comment.
Uhhh, great advice.
I'll make sure I'll get it right next time :)
There was a problem hiding this comment.
That works!
A crazy way to do it in one line (and this is why I love JS xD) would be this:
console.log("example".split("").filter(char => !"aeiou".includes(char.toLowerCase())).join(""))We're transforming "example" into an array, like you did in other exercises, and then using the .filter method on that array. The .filter method removes or keeps items from an array depending on if the condition you return is true or false (keeps the value if it is true removes it if it is false).
The condition I used inside the filter is the same one you did, the only difference is that I applied the .toLowerCase() method so I don't need to specify the vowels in upper case too.
There was a problem hiding this comment.
Nice job!
Another crazy one-line alternative solution, using the same concept as my comment in the previous exercise:
let stringComEspacos = "a a bb a";
console.log(stringComEspacos.split("").filter(char => !(char === " ")).join(""))
No description provided.