function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sowjanya SarideSowjanya Saride 

' Get Started with Apex ' running successfully but says Challenge Failed

Hello Ohana!

I am trying to pass the ' Get Started with Apex '  Challenge in
                                                  Developer Beginner  >> Apex Basics & Database >> Get Started with Apex  
Trail.

My Code runs successfuly in my Dev Console and can see the System Debug Logs.
But, it says My challenge  is  Failed. I Don't Understand . Please Help. Posting screenshots below : 
<
public class StringArrayTest {
public static List<String> generateStringArray(Integer n )
    {        
        List<String> returnStringList = new List<String>();
     
        for( Integer i = 0 ; i < n ; i++ )
        {
             returnStringList.add('Test'+i);
               
             system.debug( returnStringList[i]);
        }
        return returnStringList;
    } 
}

>
User-added image
Debug Log
User-added image
PS: I tried this in two different Playgrounds but no luck.

Thanks,
L
Raj VakatiRaj Vakati
Here is the code 
 
public class StringArrayTest {
    //Public Method
    public static String[] generateStringArray(Integer length) {
        //Instantiate the list
        String[] myArray = new List<String>();

        //Iterate throught the list
        for(Integer i=0;i<length;i++) {
            //Populate the array
            myArray.add('Test ' + i);

            // Write value to the debug log
            System.debug(myArray[i]);
        } //end loop

        return myArray;
    }//end method       
}// end class

 
Raj VakatiRaj Vakati
public class StringArrayTest {
    public static String[] generateStringArray(Integer n)

    {
        List<String> listString = new List<String>();
         for(Integer i=0;i<n;i++)
        {
           listString.add('Test '+i);
            System.debug(listString[i]);
        }
        return listString;

    }

}

 
Sowjanya SarideSowjanya Saride
Hi Raj,
Thanks for the reply. 
I copy pasted your last snippet and it worked. Thank you!
But, Can you please make me understand what is it diff from your code and mine. where am  I going wrong  or what am I missing ?
Only difference I see is, generateStringArray(Integer n) has a return type of String[] than a List - List<String>

I tried applying only this change, but still my code failed to pass the challenge. When  I copy pasted your code, it worked.
Where is that I am going wrong. Kindly reply.

Thanks,
L
Raj VakatiRaj Vakati
Logically your code is doing same. But its difference between using Array vs List  . 

My Coding is returning Array of String[] rather than list  
Sowjanya SarideSowjanya Saride
RAJ,
In your code below,
<
public class StringArrayTest {
02    public static String[] generateStringArray(Integer n)
03 
04    {
05        List<String> listString = new List<String>();
06         for(Integer i=0;i<n;i++)
07        {
08           listString.add('Test '+i);
09            System.debug(listString[i]);
10        }
11        return listString;
12 
13    }
14 
15 }
>
You are returning List of string type not an array. How is it working in this scenario. Please throw light on this.

Thank you,
L