title: Why is initializing variables so important?
body: <p>Please can someone explain to me, why NOT initializing <strong>first_idx</strong> and <strong>last_idx</strong> causes the code not to run??</p>
  
  <p>When I run it I get this error <em>"undefined local variable or method last_idx"</em>. I know that the advice is to always initialize the variables, but I don't understand why. After all first_idx and last_idx will ALWAYS get a value inside the loop because the argument <strong>lette</strong>r is always present in the string (in this particular problem).</p>
  
  <p>I'd really appreciate some (simple) insight. Thank you!</p>
  
  <p>P.S, I also know that the problem is easily solved using #index and #rindex in Ruby, but I'm not allowed to solve it using straightforward methods.</p>
  
  <pre><code>def find_for_letter(string, letter)
  
  first_idx = nil
  0.upto(string.length - 1) do |idx1|
      if string[idx1] == letter
          first_idx = idx1
          break
      end
  end
  
  last_idx = nil
  (string.length - 1).downto(0) do |idx2|
      if string[idx2] == letter
          last_idx = idx2
          break
      end
  end
  
  if last_idx == first_idx
      return [first_idx]
  else
      return [first_idx, last_idx]
  end
  end
  
  
  
  def first_last_indices(word)
      h = {}
      word.chars.each do |char|
          h[char] = find_for_letter(word, char)
      end
      h
  end
  </code></pre>