lib/is_odd.ex

defmodule IsOdd do
  @moduledoc """
  Documentation for `IsOdd`.
  Library for checking whether the number, string is odd or not.

  """

  @doc """
  Function to check a number is odd or not.
  ### Error {: .error}
  This syntax will render an error block
  
  ## Examples

      iex> IsOdd.is_odd?(5)
      true

      iex> IsOdd.is_odd?(6)
      false

  """

  use Bitwise, only_operators: true
  @spec is_odd?(integer) :: boolean
  def is_odd?(number) when is_integer(number) do
    if (number &&& 1) === 1 do
      true
    else
      false
    end
  end

  @doc """
  Function to check a string (parsable into integer) is odd or not.

  ## Examples

      iex> IsOdd.is_odd?("5")
      true

      iex> IsOdd.is_odd?("6")
      false

      iex> IsOdd.is_odd?("3FF", 16)
      true



  """
  @spec is_odd?(binary, 2..36) :: boolean
  def is_odd?(num_string, base \\ 10) when is_binary(num_string) do
    number = num_string |> String.to_integer(base)
    is_odd?(number)
  end
end