Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Lists Lists

Need some help converting my code into the TTH format. I've spent hours getting this to work and am close to giving up.

Find commented below. <> The Compiler error <> The Visual Studio code which worked

MathHelpers.cs
using System.Collections.Generic;
using System;
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static void GetPowersOf2()
        {

            int Input = int.Parse(Console.ReadLine());
            List<int> PowersOf2 = new List<int>(Input);

            for (int i = 0; i < Input ; i++)
            {
                PowersOf2.Add((int)Math.Pow(i, 2));
                Console.WriteLine(PowersOf2[i]);
                return ;
            }


        }//Does MathHelpers.GetPowersOf2 return List`1?

    }
}//WORKING VISUAL STUDIO CODE
/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {

            int InputValue = int.Parse( Console.ReadLine());
            List<int> PowersOf2 = new List<int>(InputValue);

            for (int i = 0; i < InputValue; i++)
            {
                PowersOf2.Add((int)Math.Pow(i, 2));
                Console.WriteLine(PowersOf2[i]);
            }
            Console.ReadLine();
        }
    }
}
*/

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,329 Points

Hi Oluwasomisayo Euler-Ajayi. The good news is that you understand Lists which was the goal of the lesson. Couple of minor issues here. First the challenge asks you to pass the number into the method and return a list. You instead used ReadLine and WriteLine. Easy fix there. Change your method to List<int> instead of void and pass int Input in.

Next issues took me a little debugging.

In Math.Pow the first double is the number to be raised to a power and the second number is the power. You have them backwards. So it should be

PowersOf2.Add((int)Math.Pow(2, i));

And lastly the question states "containing the powers of 2 from 0 to the value passed in". Up to the value passed in means the for loop should be <=

for (int i = 0; i <= InputValue; i++)

Good luck. Keep at it you will get it. Repost your new code if you still can't get it to pass

Thanks a bunch Mark, I spent a few hours trying different things but thanks to the added guidance I've finally finished it.

Now onto the next point ;-; Thanks a bunch :D

Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,329 Points

You are welcome Oluwasomisayo. Happy that you were able to get it to work. Thanks for letting me know you got it and for marking the answer best answer.

Keep at it and post questions in the forum if you need help. It’s a great community with a lot of people willing to help.