Recap - Reversed Array
Part of the Fundamentals section of Coddy's Ruby journey — lesson 63 of 88.
Challenge
EasyRead a single line of input containing comma-separated integers (e.g., 8,3,5,1,9).
Split the input into an array of integers, then reverse the array manually using a loop. Do not use the built-in reverse method.
Build a new array by iterating through the original array from the last element to the first, adding each element to the new array. Print each element of the reversed array on its own line.
For example, if the input is 8,3,5,1,9, the output should be:
9
1
5
3
8If the input is 10,20,30, the output should be:
30
20
10If the input is 7,2,4,6,1,8, the output should be:
8
1
6
4
2
7If the input is 5, the output should be:
5Try it yourself
# Read the comma-separated integers
input = gets.chomp
numbers = input.split(",").map(&:to_i)
# TODO: Write your code below
# Create a new array by iterating through the original array
# from the last element to the first (do NOT use the reverse method)
reversed_array = []
# Print each element of the reversed array on its own line
reversed_array.each do |num|
puts num
endAll lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input11Arrays
Creating ArraysAccessing Array ElementsModifying ArraysArray MethodsRecap - Product ArrayRecap - Reversed ArrayArray Shortcuts3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False