mardi 11 juillet 2017

Reset counter after finishing iterating with the array elements

I’m doing this code but I can’t find the proper way to reset the counter after iterating with all the elements inside the array.

The idea is that after getting to the last song in the playlist you will get into the first one. Here is my code, i would really appreciate your help with this. Thanks in advance!

class Playlist
  attr_accessor :songs
  def initialize (name,songs)
    @name = name
    @songs = songs
  end

  def display_name
    @name
  end

  def number_of_songs
    "There are #{@songs.count} songs in the #{display_name}"
  end

  def add_song(song)
    @songs << song
    @songs
  end

  def next_song
    i = 0
    while i != 9
      p "Playing song: #{@songs[i]}"
      p "skip song? (Y/N)"
      user_input = gets.chomp
      if user_input.downcase == "y" && i != 8
        i += 1
      elsif user_input.downcase == "n" && i != 8
        puts "Playing song"
        sleep(1)
        print'.'
        sleep(1)
        print'.'
        sleep(1)
        sleep(1)
        print'.'
        sleep(1)
        print'.'
        sleep(1)
        sleep(1)
        print'.'
        sleep(1)
        print'.'
        sleep(1)
        print'.'
        puts
        puts
        i+=1
      end
    end
  end
end

playlist = Playlist.new("The Ultimate Playlist",["In My Life","Naive","She Moves In Her Own Way","Skinny Love","All My Life","All The Small Things","Real"])

p playlist.display_name
p playlist.number_of_songs
p playlist.add_song("California(Here We Come)")
puts
p playlist.next_song

Aucun commentaire:

Enregistrer un commentaire