My solution for the test

I spent two days on the challenge, it was hard but happy. at first day, i spent about 7 hours to get my first version of solution, it was over 100 lines, long and ugly; i spent about 4 hours to rewrite it on the other day, it is a short and simply one, here it is:

public static List ScoreFrames(List rolls)
{
List frameList = new List();

    //your code is here
    int frameNum = 0;

    for (int i = 0; i < rolls.Count; i++)
    {
        if (i + 1 < rolls.Count )   //only add score to the list if there two bowl completed after i;
        {
            if (rolls[i] + rolls[i + 1] < 10 && frameNum < 10)  //frameNum < 10 prevents 11th frame score
            {
                frameList.Add(rolls[i] + rolls[i + 1]);
                frameNum++;
                i += 1;     //count the score every two bowl, here we increase 1 because the for loop increment condition has add one already;
            }              
            else if((rolls[i] == 10  || rolls[i] + rolls[i + 1] == 10)&& rolls.Count > i + 2)
            {
                frameList.Add(rolls[i] + rolls[i + 1] + rolls[i + 2]);
                frameNum++;
                if ( rolls[i]!=10)
                {
                    i += 1;     //if it was spare we increase as normal; if it was strike we increase only 1 bowl, which is the for loop increment condition
                }
            }
        }
    }
    return frameList;
}

Udemy_Bowling_Game_TDDChallege_pic

1 Like

Privacy & Terms