GithubHelp home page GithubHelp logo

Comments (10)

mareksuscak avatar mareksuscak commented on June 2, 2024

@taliafranco Oops, just noticed this issue.

From my understanding, (cc_number % 10 = digit1) is to obtain the last number(if im not mistaken)?

That's right.

But why is digit2 = digit1?

The key to understanding how this works lies right below that line — since digit1 is assigned AFTER its value's assigned to digit2, it means it will always contain the value of digit1 from the previous loop iteration. Therefore, after completing all iterations it will hold last but one value processed — the second digit since we progress from the end to the start of the number.

and why sum_of_even += digit1?

I assume you're referring to the following lines of code:

if (num_digits % 2 == 0)
{
sum_of_evens += digit1;
}

According to the algorithm description, point number 2 and the relevant portion of the point number 1:

  1. Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.
  2. Add the sum to the sum of the digits that weren’t multiplied by 2.
  3. If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

We need to add all digits that weren't multiplied by 2. According to the definition, the other digits are odd and those that were not multiplied by two must be even thereof.

Hope it helps.

from cs50.

karkelley avatar karkelley commented on June 2, 2024

The problem I have with the explanation is that the generic word "it" is used too often. I cannot figure out which part "it" is referring to in each line. Some clarification here would help.

from cs50.

rajan9519 avatar rajan9519 commented on June 2, 2024

from cs50.

karkelley avatar karkelley commented on June 2, 2024

from cs50.

rajan9519 avatar rajan9519 commented on June 2, 2024

from cs50.

karkelley avatar karkelley commented on June 2, 2024

from cs50.

rajan9519 avatar rajan9519 commented on June 2, 2024

from cs50.

karkelley avatar karkelley commented on June 2, 2024

from cs50.

rajan9519 avatar rajan9519 commented on June 2, 2024

from cs50.

karkelley avatar karkelley commented on June 2, 2024

from cs50.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.