Skip to content

Instantly share code, notes, and snippets.

@prihandi
Created January 15, 2018 04:51
Show Gist options
  • Save prihandi/1ce1b4c60b2d96db9c3caabcf9f109b7 to your computer and use it in GitHub Desktop.
Save prihandi/1ce1b4c60b2d96db9c3caabcf9f109b7 to your computer and use it in GitHub Desktop.

Revisions

  1. prihandi created this gist Jan 15, 2018.
    6 changes: 6 additions & 0 deletions isbn_13.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    def create_isbn13(isbn_12)
    isbn_arr = isbn_12.to_s.chars.map(&:to_i)
    sumarr = isbn_arr.each.with_index.inject(0) { |sum,(value,index)| index.even? ? sum+(value*1) : sum+(value*3) }
    last_digit = (10 - (sumarr % 10)) % 10
    isbn13 = isbn_12.to_s + last_digit.to_s
    end