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
Jayesh SonawaneJayesh Sonawane 

Trailhead Challenge : Not able to test "Apex Basics & Database" Challenge !

The challenge is to Create an Apex class naming StringArrayTest having function generateStringArray that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.

So I  have created following StringArrayTest Class with generateStringArray function.

public class StringArrayTest {
    public static String[] generateStringArray(Integer n)
    {
        String[] testList = new List<String>();
            for (Integer i=0;i<n;i++)
            {
                testList.add('Test'+i);
            }
            return testList;
    }
}

But when i click on Ckeck Challenge. it is showing following error : 
Challenge Not yet complete... here's what's wrong: 
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.

Whats Wrong in here ?
Rahul SherikarRahul Sherikar

Hello  Jayesh Sonawane,

Use below code that i modified from your code,its working.


public class StringArrayTest
{
    public static list<string> generateStringArray(Integer n)
    {
        list<string> testList = new List<String>();
            for (Integer i=0;i<n;i++)
            {
                testList.add('Test '+i);
            }
            return testList;
    }
}
Vijay NagarathinamVijay Nagarathinam
Hi Jayesh,

Use the below it will work,
 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}

Let me know if you need any help regarding this.

Thanks,
VIjay
Jayesh SonawaneJayesh Sonawane
Thanks Rahu, Vijay ... it works,
But what was wrong in my code , in Challenge's problem statement it was written that the function can return list or string array ! , but it is was not accepting string array. 

Thanks for your quick reply