Rebuilding the Cycle Method to Repeatedly Go Through an Array

This coding exercise examines how to iterate through the same array a variable number of times and return a collection that contains the full set of elements that were looped through.

large

View Video

Summary

Replicate Ruby's cycle method.

Exercise File

Code File

Exercise Description

Build a method that replicates Ruby's cycle method and iterates through an array a variable number of times, and have the method return an array that contains the full set of elements that were iterated through.

Example

cloned_cycle([1, 2, 3], 3)
# [1, 2, 3, 1, 2, 3, 1, 2, 3]

Real World Usage

Understanding how arrays work in Ruby is key to working with data collections, which is one of the most fundamental requirements of data-driven applications. This exercise will help you learn how to work with nested collections, add elements to an array, and override the basic looping functionality provided by collections.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

Jordan Hudgens

I've been a software engineer for the past decade and have traveled the world building applications and training individuals on a wide variety of topics.


View All Posts