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

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

The item at index 1 should be 2

So I'm getting the error message item at index 1 should be 2. The index 1 is equal to 2, so I don't understand why I get this error.

MathHelpers.cs
using System.Collections.Generic;

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static List<int> GetPowersOf2(int num)
        {
            return new List<int>(5){
              (int)System.Math.Pow(num, 0),
              (int)System.Math.Pow(num, 0.5),
              (int)System.Math.Pow(num, 1),
              (int)System.Math.Pow(num, 1.5),
              (int)System.Math.Pow(num, 2)
            };

        }
    }
}

// Error message: item at index 1 should be 2.

// This returns list as 1, 2, 4, 8, 16 if the value 4 is passed in.

1 Answer

Steven Parker
Steven Parker
218,583 Points

Here's a few hints:

  • the list should contain "powers of 2" (not powers of "num")
  • the list should have a variable size "...from 0 to the value passed in"
  • you won't need any fractional exponents
  • you might need to use a loop