Not sure why my method isn't working - HELP!

ruby

#1

Trying a side project out! Need help. Not sure why it's not working. This method is supposed to get a string, and return that string with it's previous letter. For example, "afe" should return "zed"

def decrypt(string)
  i = 0
  j = 0
  alphabet = "abcdefghijklmnopqrstuvwxyz"
  new_string = ""

  while i < string.length
    while j < alphabet.length
      if string[i] == " "
        puts string[i] = " "
      elsif string[i] == alphabet[j]
        new_string = new_string + alphabet[j-1]
      end
      j += 1
    end
    i += 1
  end
  puts new_string
end

decrypt("aje")

#2

Argue for why it would work, and then find out if it's doing those things

Also, you have a method, it should probably be returning something rather than be printing something (otherwise it's both printing and processing, it should be doing one thing)


#3

I'm pretty terrible at keeping track of what variables are doing sometimes. To help me out, I put in statements so that I can easily see what is going on. You might want to give it a try. Start out with something like:

  while i < string.length
    puts "-----Just inside the i loop"
    puts "i = #{i}"
    puts "j = #{j}"

See if you get what you are expecting.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.