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
Boudewijn TimmersBoudewijn Timmers 

Trailhead Getting Started With Apex not correct?

Hi there,

have an issue with the first Apex challenge in the Developer beginner module. As far as I can see I did it all right but the challenge won't accept. I need to create a list of strings with format 'Test n' where n is the number of the entry in the list. The lenght needs to be defined through a parameter. Here's my code (including a debug which shows me the correct strings):

public class StringArrayTest {
    Public static void generateStringArray (Integer provide_count) {
        List<String> TestString = new List<String>();
        for(Integer i=0;i<provide_count;i++) {
            TestString.add('Test '+string.valueof(i));
            System.debug(TestString);
        }
    }
}

What am I doing wrong here?

regards,
Boudewijn
Best Answer chosen by Boudewijn Timmers
sfdcMonkey.comsfdcMonkey.com
hi Boudewijn
try this 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;
    }
}
Thanks
Mark it best answer if it helps you :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Boudewijn
try this 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;
    }
}
Thanks
Mark it best answer if it helps you :)
 
This was selected as the best answer
Boudewijn TimmersBoudewijn Timmers
Hi Soni,

thanks, that worked! Basically I can see two differences:
  1. in adding the string to the array you don't use string.valueof(i) but just i; does apex simply convert the variable format? I am used to pascal in the old days which was much more restrictive and wouldn't let me do it like this :)
  2. line 11 which returns the Array; is this to return the array to the generateStringArray method?
Best regards,
Boudewijn
sfdcMonkey.comsfdcMonkey.com
hi
yes this method return type is List<String> so in the end we use return myArray; statement so our generateStringArray method return an string array link
test 1, test 2, test3, ....test n..
i hop it helps you :)
Please mark it best answer if it helps you :)
Thanks