Rosetta Code

Middle three digits

Return the middle three digits of integers that have an odd number of digits.

Intro View source
Source rosettacode/popular/middle_three_digits.vibe
# title: Middle three digits
# source: https://rosettacode.org/wiki/Middle_three_digits
# category: Rosetta Code
# difficulty: Intro
# summary: Return the middle three digits of integers that have an odd number of digits.
# tags: popular, numbers, strings, validation
# vibe: 0.2

def substring(text, start_index, count)
  output = ""
  offset = 0

  while offset < count && start_index + offset < text.length
    output = output + text.slice(start_index + offset)
    offset = offset + 1
  end

  output
end

def middle_three_digits(value)
  digits = "" + value.abs

  if digits.length < 3
    return "too short"
  end

  if digits.length.even?
    return "even digit count"
  end

  middle = (digits.length - 3) / 2
  substring(digits, middle, 3)
end

def run
  rows = []
  rows = rows.push(["input", "middle_three"])
  rows = rows.push([123, middle_three_digits(123)])
  rows = rows.push([12345, middle_three_digits(12345)])
  rows = rows.push([1234567, middle_three_digits(1234567)])
  rows = rows.push([10001, middle_three_digits(10001)])
  rows = rows.push([-10001, middle_three_digits(-10001)])
  rows = rows.push([1, middle_three_digits(1)])
  rows = rows.push([2002, middle_three_digits(2002)])
  rows
end
Output
Press run to execute run from this example.
rosetta-code popular numbers strings validation browser-runner