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
ux5ux5 

Trailhead Challange not yet complete- (Getting started with APEX module)

On the getting started with APEX, I cannot complete the challange and I am receiving the 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.

But in the Debug Execute Anonymous Window, I can successfully run the code.  Here is the code used in the Anonymous window.

List<String> rtnStr = StringArrayTest.generateStringArray(4);
System.debug(rtnStr);
System.debug('rtnstr.size='+rtnstr.size());
As prevously said, if I do a Check Challange from the trailhead module I recieve the error.  In looking at the log there are two suspicious lines.
18:53:07:031 EXCEPTION_THROWN [1]|System.AssertException: Assertion Failed: Expected: Test 1, Actual: Test 0
18:53:07:031 FATAL_ERROR System.AssertException: Assertion Failed: Expected: Test 1, Actual: Test 0  
Can anyone help with this.  Adding a test case did not remedy the problem but I may have done something wrong there.  

And here is my code:
public class StringArrayTest {

    public static String[] generateStringArray(Integer count){
        List<String> mylist = new List<String>();
        for (Integer i=1; i<=count; i++) {
            String myStr = 'Test '  + i;
            System.Debug('Created '+ myStr);
            mylist.add(myStr);
        }
        System.debug('Completed!');
        return mylist;
    }

}

Question is:  Should I have to have a test case to complete this challange or have I got something else wrong?

TIA for your help
ux5
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Please check below post. I hope that will help u.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AvPBIA0

Please try below code:-
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;
    }
}
I hope you need to start loop from 0 to <n
Please let us know if this will help u

Thanks
Amit Chaudhary
William TranWilliam Tran
You are close, use 0 to <n, here's my class, it passed!
thx.
 
public class StringArrayTest{

    public static List<String> generateStringArray(Integer n) {
        List<String> ListFields = new List<String>();
        String s;
        integer x;
        for (x=0;x<n;x++){
            s= 'Test ' + x;
            ListFields.add(s);}  
            return(ListFields);
    }
}

 
Punima SharmaPunima Sharma

Hi ux5,

Below is the code I used to complete my trailhead challenge:

public class StringArrayTest {

    public static String[] generateStringArray(integer length) {
        
        String[] myArray = new List<String>();
        
        for(integer i = 0;  i < length;  i++) {
            
         myArray.add('Test '+ i);
            
            System.debug(myArray[i])  ; 
        }        
        return myArray;
    }
}
Ben KingsleyBen Kingsley
Hey Guys,

This is what worked for me.  Enjoy!

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n)
    {
        List <String> ListFields = new list<String>();
        String s;
        integer x;
           for(x=0;x<n;x++){
        s='Test ' + x;
            ListFields.add(s);
        }
        return(ListFields);
    
    }
}