2 Collect `Em All


#1

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect {|n| n ** 2}

This code still seems to give me an error about not having 2 in it, can anyone help or explain . I read the other two topics, but can't seem to figure it out .


#2

SOLVED by thinking outside the box a little. Here's the code I had to write to get a green button finally to light up when the previous code gave a continuous error message about not having 2 in it :

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

double = Proc.new {|x| x * 2}

doubled_fibs = fibs.collect(&double)

just used the proc from lesson 7 :smile:


#3

wow, thanks novik21, ha!


#4

usei seu codigo, apenas trocando a potenciaĆ§Ć£o pela multiplicaĆ§Ć£o e funcionou.
doubled_fibs = fibs.collect {|n| n * 2}


#6

you don't need Proc at all. (i haven't studied that yet)
here's my code!

y = fibs.collect { |x| 2*x}

doubled_fibs = y

side note - This task is little tricky in the sense that it wont work if you put "puts" to print the result


#7

For a one line fix, this works lovely..

doubled_fibs = fibs.collect {|x| x * 2}


#8

This is what I used guys and it worked perfect.
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

doubled_fibs = fibs.collect{|fibs| fibs * 2}


#9

Actually if you put the "puts" before the y = fibs.collect{|x| x*2} it should work iirc. Also as a side note 2*x is generally considered really bad form in most programming languages. To be honest, it's something I had learned long ago but I can't remember "why" it was bad. I think it had to do with readability.


#10

Thanks for correcting me with the right form. Yeah "puts" definitely does that job but iirc, that exercise had a bug. So, instead it would give me an error. Some exercises in here is buggy with Mozilla FF.


#11

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

doubled_fibs = fibs.collect { |num| num * 2 }

doubled_fibs


#12

I just did it like this

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

doubled_fibs = fibs.collect {|x| x*2}


#13

It seems a lot of people (including me... thats why I am here) confused doubled_fibs (multiplied by 2) as if they were asking for squared_fibs (raised to the power of 2). When code academy instructions said "similar to the example above" didn't mean "exactly as".


#14

First tried:
doubled_fibs = fibs.collect {|n| n **2}

This gave an error so i tried a lot of different things, but here below is my working code.

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

doubled_fibs = fibs.collect {|n| n *1}
doubled_fibs.collect! { |n| n *2}


#15

Here's my code it's giving me an error saying "Did you create a varible double_fibs?" Not sure what's wrong here.

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

double_fibs = fibs.collect { |x| x * 2 }


#16

try this code
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
doubled_fibs=fibs.collect {|num| num*2}

doubled_fibs


#17

Here is my code. Works for me!

fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

doubled_fibs = fibs.collect { |x| x * 2}