Exercise Palindromo Javascript

Asked

Viewed 122 times

0

I would like to understand this code as simplistically as possible.

var palindromo = "";

for(var i = palavra.length - 1; i >= 0; i--) {
   palindromo += palavra[i]
}

if (palavra == palindromo) {
   return "SIM, SOU UM PALÍNDROMO"
} else {
   return "INFELIZMENTE, NÃO SOU UM PALÍNDROMO"
}

I know that on the first go he does the reverse check.

But I want to understand the process of that part:

palindromo += palavra[i]

What the word EGG and the word YES would look like.

Seria:

Position of the word YES

0 = S 1 = I 2 = M

3 - 1 = heading 2 which is the letter’M'

palindromo += palavra[i]

"" = "" + YES [M] - Would that be it? How does this part?

  • 2

    This answers your question? Function to check palindrome

  • 1

    palavra[i] picks up the character that is in position i string palavra, then when i vale 2, the expression palavra[i] results in a string containing only the letter "M". And as the operator +, when applied to strings, does the concatenation, then the expression palindromo += palavra[i] is adding the character at the end of the string palindromo. Therefore, the loop adds the last letter, then the penultimate and so on, to the first. At the end the string palindromo contains the characters of palavra in reverse order, and if both are equal, it is because the string is a palindrome.

  • I put as an answer what I thought.

  • 2

    Actually, the index is not "M" or "I", and yes 2 or 1, etc. So when i vale 2, the expression palavra[i] is "translated" as palavra[2], which is the character that is in the position 2 (i.e., the letter "M"). And in the third step, "MI" + "S" results in "MIS", not "SMI" (each new character is added at the end)

  • But the idea is exactly what I put in the answer?

1 answer

5


You initialize the "palindrome" variable as an empty string -> ""

var palindromo = "";

Make a loop by the size of the variable "word", in this case, let’s assume that: var word = "YES"

for(var i = palavra.length - 1; i >= 0; i--) {
   palindromo += palavra[i]
}

This loop is little bit different, you start with "var i = palavra.length - 1;" "YES" has 3 letters, minus 1 = 2, so var i = 2 As long as i is greater than or equal to 0, you will decrease its value, i.e., i = i - 1

Inside the loop you perform this: palindromo += palavra[i] In the first iteration of the loop its i is equal to 2 and palindrome equal to "" A string in javascript can be treated as an array, so when you run palavra[i] in that case palavra[2] you are selecting the letter "M" which is the position [2] of "YES". And increments at the end of the variable "palindromo"

  1. In the first interaction -> i = 2, palindrome = "" and ends as "M"
  2. In the second interaction -> i = 1, palindrome = "M" and ends as "MI"
  3. In the third interaction -> i = 0, palindrome = "MI" and ends as "MIS"

Now you simply compare whether the original word is equal to the generated palindrome, for example "SIM" == "MIS" = false and "OVO" == "OVO" = true

if (palavra == palindromo) {
   return "SIM, SOU UM PALÍNDROMO"
} else {
   return "INFELIZMENTE, NÃO SOU UM PALÍNDROMO"
}
  • 2

    i = 0 right? In item 3. you put.

  • I get it, but I want to know the way I put it in the answer how it would look, got it, I’ll put another answer that maybe is what you mean.

  • I will put more questions like this, I like to understand the code straight. See you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.