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

Aaron Selonke
Aaron Selonke
10,323 Points

Getting Past the C# List<T> 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<double> GetPowersOf2(int input)
        {
            var returnList = new List<double>();
            for (int i = 0; i < input+1; i++)
            {
                returnList.Add(Math.Pow(2, i));
            }
            return returnList;

        }

    }
}

10 Answers

The challenge is confusing in that it wants the List returned to the method to be of type int, however Math.Pow() uses type double for its parameters. Your solution above is close, all you have to do is change each instance of List<double> to List<int> and then use typecasting on your Math.Pow() method, as seen below

for(int i = 0; i <= input; i++)
{
    returnList.Add((int)Math.Pow(2, i))
}
Rui Martinho
Rui Martinho
21,221 Points

It worked.

Thank you, Zachary

Aaron Selonke
Aaron Selonke
10,323 Points

Nice, You can read a code challenge - there was nothing that would have made me guess that it is expecting a list of int.

I do not see any issue in the code. But try declaring the List<double> returnList = new List<double>(); instead of var. Also try if the code may be validating for a List of intergers. try converting the power to an integer and then store it to the List of Intergers than double values.

Nice catch! :)

Try using for(int i=0; i <= input; i++)

Actually,

for (int i = 0; i < input + 1; i++)

And

for (int 1 = 0; i <= input; i++)

Are exactly the same.

Martin Sole
Martin Sole
82,199 Points

I had the same issue, but managed to pass the challenge by using int for the list type instead of double and casting the returnList to an int,

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

I'm rather new to C#, but maybe try getting rid of the +1 from this: for (int i = 0; i < input+1; i++)

Good luck! ~Alex

Rui Martinho
Rui Martinho
21,221 Points

I am experiencing the same problem.

It returns the message "Error: " Bummer! Does MathHelpers.GetPowersOf2 return List`1?""

I've also got the same problem and almost identical code. When I compile and run locally, it produces a list of doubles with the correct values, so it seems to be a bug in the challenge. Has anyone reported it yet?

Chanuka Weerasinghe
Chanuka Weerasinghe
2,337 Points

Just adding this answer even after 4 months because I had the same issue and came here to see if there was a bug in the task. Lol

Just to cofirm no bug in this task. Although poorly written question as it seems.

This is the complete solution -

        int num = Convert.ToInt32(userInput);

        List <int> numList = new List <int>();

        for (int x = 0; x <= num; x++)
        {
            numList.Add((int)Math.Pow(2, x));
        }

return numList;

using System.Collections.Generic;
using System;

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

        }

    }
}
Kevin Gates
Kevin Gates
15,053 Points

Alternate solution:

using System.Collections.Generic;
using System;
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static List<int> GetPowersOf2 (int value)
        {
            List<int> returnList = new List<int>(value+1);
            for (int i = 0 ; i < returnList.Capacity; i++)
            {
                returnList.Add(Convert.ToInt32(Math.Pow(2, i)));
            }
            return returnList;
        }
    }
}