Cyfrin / foundry-full-course-cu

GNU General Public License v3.0
3.72k stars 923 forks source link

Two valid answers for a quiz on Foundry Fundamentals, Section 4.11 #2809

Closed yawarasuuna closed 3 weeks ago

yawarasuuna commented 3 weeks ago

Discussed in https://github.com/Cyfrin/foundry-full-course-cu/discussions/2768

Originally posted by **yawarasuuna** October 28, 2024 One of the question on Foundry Fundamentals, Section 4.11 for "Quiz: Smart Contract Lottery Intro", it asks "How can you use the modulo operator (%) to determine if a number is even or odd?" ![Screenshot 2024-10-28 at 15-38-07 Quiz Smart Contract Lottery Intro Cyfrin Updraft](https://github.com/user-attachments/assets/4e381de0-e2c2-4ce1-a83e-9af5d8d95986) Wouldn't both answers B and C, in the example above, allow us to determine if a number is even or odd? ``` if n % 2 != 1 // If n is not odd ( n is even), otherwise it's odd ``` ``` if n % 2 == 0 // If n is even, otherwise it's odd ``` From previous discussion linked above, it might indeed be true, and I wasn't hallucinating with the AI. I believe this might be the correct repo with the challenges, but it is set to private on my side: https://github.com/Cyfrin/cyfrin-updraft-challenges-private Thank you,
EngrPips commented 3 weeks ago

Hello @yawarasuuna, Thanks for reporting this. It will be responded to appropriately as soon as possible.

Equious commented 3 weeks ago

It's likely a technicality in the world of blockchain, but the general convention is if n % 2 == 0. The reason for this is that there are instances of n % 2 which may not strictly return a 0 or a 1. An easy example is floating point numbers, which of course isn't generally a consideration on chain, but complex number types run into similar issues. The recommendation is to stick with the n % 2 == 0 convention, alternatives may work with a bunch of caveats.

Choose the best answer!