Skip to content

segundo capítulo#2

Open
gxnca wants to merge 2 commits intomainfrom
gf/chapter2
Open

segundo capítulo#2
gxnca wants to merge 2 commits intomainfrom
gf/chapter2

Conversation

@gxnca
Copy link
Copy Markdown
Member

@gxnca gxnca commented Nov 19, 2024

No description provided.

@gxnca gxnca requested review from diogogmatos and nunom27 November 19, 2024 23:54
function Palindromo(s) {}
function Palindromo(s) {
const reversed = s.split("").reverse().join("")
if (reversed == s) {
Copy link
Copy Markdown
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
Copy Markdown
Member 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect 👌🏻

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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