+ Start a Discussion
Disha SainiDisha Saini 

Trailhead: Developer Beginner -Get started with Apex module- challenge failing

Hi,
Though i have coded the with the same specification as asked in the challenge but then also challenge failing.
Not sure why this is happening. The issue is actually the code or Trailhead is not working fine.

I am a beginner so facing issue in identifying it.
Need help.

Thanks in advance. 
Best Answer chosen by Disha Saini
RKSalesforceRKSalesforce
Try this one:
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;
    }
}

 

All Answers

RKSalesforceRKSalesforce
Hi Disha,

Please paste your code here.

Regards,
Ramakant
Skype : ramakant_sul
Disha SainiDisha Saini
Hi Ramakant,

Below is the SS of error and code.
User-added image

public class StringArrayTest {

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

Please let me know whats going wrong here.
RKSalesforceRKSalesforce
Hi Disha,

Please try below code:
public class StringArrayTest {

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

Please mark as best answer if helped. Please let mek now if you need any further assistance.

Regards,
Ramakant​
Disha SainiDisha Saini

Its giving the same error with the above code as it was giving before.

 

RKSalesforceRKSalesforce
Try this one:
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;
    }
}

 
This was selected as the best answer
Disha SainiDisha Saini

It worked now :)

understood the issue, for loop condition wasn't matching 
i used i<=n and because of this loop was running for n+1 time 

Thanks Ramakant

Disha SainiDisha Saini
Sure.
Sanniminni SakariSanniminni Sakari
Hi,
I'm struggling with this same challange, and even if the code Ramakant gave makes sense, there is still something wrong with my challange as I get message method either not existing, not static or not giving the asked return.

BR, Sanniminni
Sanniminni SakariSanniminni Sakari
EDIT: does it matter that in my code the word "static" is not with the green colour? Maybe there is something worng there as the debugger says:
"static can only be used on methods of a top level type".

BR, Sanniminni
John Murphy 38John Murphy 38
I also tried Ramakant's code, was getting the same error as Sanniminni in the Execute Anonymous window.  I thought it maybe something that just shows there so I clicked the "Check Challnege" button in Tralihead anyway.  It gave me an eorr saying "No Apex class named 'StringArrayTest' was found"  
Keerthi SfcKeerthi Sfc
trailhead is kind a tricky. The issue is the expectation to display the list as Test 1, Test 2 ...

So check wheteher u are using Capital 'T' and space after Test : it should solve the issue if you are using exact above code.

a.add('Test  '+i);