Skip to content

Commit

Permalink
fix(luhn): fix the check by reversing ordering (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantJoshi00 authored Nov 24, 2023
1 parent fae88a7 commit 61c0164
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ pub fn luhn_on_string(number: &str) -> bool {
pub fn luhn(number: &[u8]) -> bool {
number
.iter()
.rev()
.enumerate()
.map(|(idx, element)| {
((*element * 2) / 10 + (*element * 2) % 10) * (((idx + 1) as u8) % 2)
+ (*element) * ((idx as u8) % 2)
((*element * 2) / 10 + (*element * 2) % 10) * ((idx as u8) % 2)
+ (*element) * (((idx + 1) as u8) % 2)
})
.sum::<u8>()
% 10
Expand Down

0 comments on commit 61c0164

Please sign in to comment.