-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
segundo capítulo #2
base: main
Are you sure you want to change the base?
Conversation
function Palindromo(s) {} | ||
function Palindromo(s) { | ||
const reversed = s.split("").reverse().join("") | ||
if (reversed == s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhhh, great advice.
I'll make sure I'll get it right next time :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect 👌🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(""))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
No description provided.