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

Calvin Secrest
Calvin Secrest
24,812 Points

Getting Past the C# List<T> for list of integers Challange

The code below works fine in the IDE, but will not let me pass the challenge.

Directions: "Create a static method named GetPowersOf2 that returns a list of powers of 2 from 0 to the value passed in. So if 4 was passed in, the function would return { 1, 2, 4, 8, 16 }. The System.Math.Pow method will come in handy."

Error: " Bummer! Does MathHelpers.GetPowersOf2 return List`1?"

Does anyone now what the Code Challenge is looking for (besides what its asking)

Thanks

MathHelpers.cs
using System.Collections.Generic;
using System;

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static List<int> GetPowersOf2(int input)
        {
            var returnList = new List<double>();
            for (int 1 = 0; i <= input; i++)
            {
                returnList.Add((int)Math.Pow(2, i));
            }   
        }
    }
}

2 Answers

Hi,

I hope the following tips helps out to solve the challenge:

  • The GetPowersOf2 method performs actions but it doesn't include a return statement to return the List
  • Variable names cannot start with a number, so in the for loop the variable cannot be called 1, I guess what you wanted was to call it i
  • In the signature of the method it says a list of int will be returned, but the returnList type is a list of double

Many thanks. Have a nice day, edu

Calvin Secrest
Calvin Secrest
24,812 Points

Thank you for this assistance!

You're welcome :)