Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Having problems adding to a array

The goal is to add to the array w however it is also suppose to calculate if it's a integer or string. Also having a problem shifting and keeping the loop going. Here is my syntax

array = [1,2,3,4,5] w = []

def iterate(name) if name.empty? || name.nil? puts"Sorry there is nothing in array." else name.each do |o| print o end end end

def add_to_array(arg) puts"Type in something to add in this array." name = gets.chomp if name.is_a?(Integer) arg.push(name.to_i) puts"added number" else
arg.push(name) puts"Added string" end end

def shifter(par) puts"removed one" par.shift puts "Would you like to remove another?(YES?NO)" h = gets.chomp i = 0 while h == "yes" par.shift if h == "no" break end end end

iterate(array) iterate(w)

add_to_array(w)

puts"add another"

add_to_array(w) puts print w

add_to_array(w)

print w

shifter(w)

print w

1 Answer

Let me know if this helps.

array = [1,2,3,4,5] 
w = []
# Added my own is_number? method because 'gets.chomp' or just 'gets' method will always return a string
# with is_number? method we are checking if the characters inside the string are numbers
# if true then we will convert it to an integer and push it on the array

class String 
    def is_number?
      true if Float(self) rescue false
    end
end


def iterate(name) 
    if name.empty? || name.nil? 
        puts"Sorry there is nothing in array." 
    else 
        name.each { |o| print "#{o} \n" }
    end
end


def add_to_array(arg) 
    puts"Type in something to add in this array."
    name = gets.chomp

    if name.is_number?
        arg.push(name.to_i) 
        puts"added number" 
    else
        arg.push(name) 
        puts"Added string" 
    end 
end


def shifter(par)
    par.shift  
    puts"removed one"
    puts par

    # This loop will always run once so the user will be asked if they want to delete another
    # The loop will continue running and shifting the array until the user responds with 'NO' even if the array is empty
    loop do
        puts "Would you like to remove another?(YES?NO)" 
        h = gets.chomp 
        if h == "NO" 
            break 
        else 
            par.shift
        end
    end
end

iterate(array) 
iterate(w)

add_to_array(w)

puts"add another"

add_to_array(w) 
puts print w

add_to_array(w)

print w

shifter(w)

print w