Arrays: Generate a Landscape

preview_player
Показать описание
Swift Playgrounds for iPad, Learn to Code 1 & 2, Real-time Walkthrough and Solution
Рекомендации по теме
Комментарии
Автор

For heights, both your (& mine) and the Apple solution don't seem to allow 0 blocks at a coordinate. The "for i in 0...heights[index]" loop runs at least once if the heights[index] value is 0, meaning one block is placed at that location. If the heights[index] value is 5, it will place 6 blocks.

In the Apple solution and in the pre-written code that we are to modify, it uses "for i in 0...heights[index]", but in the text preceding the code at the beginning it gives an example:

var heights = [7, 3, 2, 4]
for i in 1...heights[0]

It say this loop will run 7 times because index 0 is 7. That is OK, because the for loop starts at 1, not 0 (e.g. "for i in 1...", not "for i in 0...")

This is fine and if I change the code so that it reads "for i in 1...heights[index]", it will place the correct number of blocks at each coordinate. However, it only works if all values in the heights array are >0. If I specify a 0 value anywhere, i get an error when I try to run the code.

"Problem Running Playground. There was a problem encountered while running this playground. Check your code for mistakes"

I would be interested to know how one could specify 0 blocks at a coordinate and get 0 blocks.

BTW, thanks for this series of videos. I was going fine until I encountered the Arrays section and was about to give up. I discovered your videos and that got me through the ones I was stumbling on, so I'm still on board with the playgrounds learning tool.

sryangm
Автор

How about some audio explaining whats going on?

bsorby