Complete the code to transform each number by doubling it using map.
numbers = [1, 2, 3, 4] doubled = numbers.[1] { |n| n * 2 } puts doubled.inspect
each which returns the original array without changes.select which filters elements instead of transforming.The map method transforms each element and returns a new array with the results.
Complete the code to convert all strings in the array to uppercase using collect.
words = ['apple', 'banana', 'cherry'] upcased = words.[1] { |word| word.upcase } puts upcased.inspect
each which does not return a transformed array.select which filters elements.The collect method is an alias for map and transforms each element.
Fix the error in the code to correctly transform numbers by adding 5 using map.
nums = [10, 20, 30] result = nums.[1] { |num| num + 5 } puts result.inspect
each which does not transform the array.select which filters elements.map returns a new array with each element transformed. each returns the original array.
Fill both blanks to create a hash where keys are words and values are their lengths using map.
words = ['dog', 'cat', 'bird'] lengths = words.[1] { |word| [word, word.[2]] }.to_h puts lengths.inspect
each which does not return a transformed array.size which also works but is less common than length.map transforms each word into a key-value pair array. length returns the length of the string.
Fill all three blanks to transform an array of numbers into a hash with keys as strings and values as squares.
nums = [1, 2, 3] squares = nums.[1] { |n| [n.[2].to_s, n [3] 2] }.to_h puts squares.inspect
each which does not transform the array.* instead of ** for squaring.map transforms each number into a key-value pair. to_s converts the number to a string key. ** squares the number.