Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

segundo capítulo #2

wants to merge 2 commits into from

Conversation

gxnca
Copy link

@gxnca gxnca commented Nov 19, 2024

No description provided.

@gxnca gxnca requested review from nunom27 and diogogmatos November 19, 2024 23:54
function Palindromo(s) {}
function Palindromo(s) {
const reversed = s.split("").reverse().join("")
if (reversed == s) {
Copy link
Member

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 ;)

Copy link
Author

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 :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 👌🏻

Copy link
Member

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.

Copy link
Member

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(""))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants