Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 33,558 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 33,558 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.