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# Basics (Retired) Perfect Final

Stuck at Final Objective 1: Code taking too long to run

I've wrote this for the first objective but I keep getting the error that the code is taking too long to run. Can't proceed to loop printing of "Yay!" for x times user enters.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int n = 0; 
            string input = Console.ReadLine();

            int counts = int.Parse(input);

            while(n < counts)
            {
              Console.WriteLine("Yay!");
              counts++;
            }

        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,236 Points

I wonder if Caleb has ever heard that "insanity is doing the same thing over and over and expecting a different result."

But you had the right idea, you just forgot that n is your loop counter and counts is your target. Inside the loop, you need to increment your counter, not the target, like this:

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int n = 0; 
            string input = Console.ReadLine();

            int counts = int.Parse(input);

            while(n < counts)
            {
              Console.WriteLine("Yay!");
              n++;        // <-- increment the counter here
            }
        }
    }
}
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

I have not heard that quote. And I am not surprised it was the code. I don't know any C# though, so that was my best guess.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Copy the code, go back to the overview of the course, click on the link to the challenge, then paste the code into the challenge. Try to run it again.