Expression "n % 2 ? ..." should not be "n % 2 == 1 ? ..."

Asked

Viewed 159 times

1

Hello, I did an exercise in codewars and a solution caught my attention:

function padIt(str, n) {
  while (n > 0) {
    str = n-- % 2 ? '*' + str : str + '*';
  }
  return str;
}

I wonder, what exactly the condition within the while does.

More specifically the condition n-- % 2 ? ..., shouldn’t be n-- % 2 == 1 ? ...

  • 1

    If 1 is already true, you don’t need the comparison. See more details in the yellow frame links.

  • @Bacco thank you so much! I’m sorry for asking a duplicate question, I must delete it?

  • 1

    Duplicates are for indexing. As it is positive, I see no reason to remove.

No answers

Browser other questions tagged

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