This was the first exercise:
Complete the missing code to process the parallel arrays from the first page of the playground.
And I've created a code:
let songTitles = ["Ooh yeah", "Maybe", "No, no, no", "Makin' up your mind"]
let artists = ["Brenda and the Del-chords", "Brenda and the Del-chords", "Fizz", "Boom!"]
let durations = [90, 200, 150, 440]
func songInformation(title: String, artist: String, duration: Int) -> String {
return "The song \(songTitles) by \(artists) has a duration of \(duration) seconds"
}
for i in 0 ... songTitles.count - 1 {
print(songInformation(title: songTitles[i], artist: artists[i], duration: durations[i]))
}
Then is the next exercise:
Below, use the Song struct from the previous page to simplify your code.
This is a struct:
struct Song {
let title: String
let artist: String
let duration: Int
}
And this is my code that doesn't work correctly:
/* Create the array of songs here */
let songs = ["Diamonds", "Animals", "Summer", "Now or never"]
/* Declare the songInformation function here */
func songInformations(song: String) -> String{
return "I love a song \(songs)"
}
/* Write a for...in loop here */
for a in 0 ... songs.count - 1 {
let song1 = Song(title: songs[a], artist: "Rihanna", duration: 100)
print(songInformations(song: song1))
}
Please help me to do this exercise. I don't understand what is wrong
What does not work ? What do you expect ? What do you get ?
Probably, exercise asks to use struct: Song.
So, you have to change songs to be an array or Song.
You can first create the songs (with the previous list):
/* Create the array of songs here */
let songDiamonds = Song(title: "Ooh yeah", artist: "Diamonds", duration: 90)
let songBrenda = Song(title: "Maybe", artist: "Brenda and the Del-chords", duration: 200)
let songFizz = Song(title: "No, no, no", artist: "Fizz", duration: 150)
let songBoom = Song(title: "Makin' up your mind", artist: "Boom!", duration: 440)
let songs : [Song] = [songDiamonds, songBrenda, songFizz, songBoom]
/* Declare the songInformation function here */
func songInformations(song: Song) -> String {
return "I love this song \(song.title) by \(song.artist). I can listen to it for \(song.duration) minutes."
}
/* Write a for...in loop here */
for song in songs {
print(songInformations(song: song))
}