• Siddhant Rathore
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hello all,
i am trying to pass this trailhead code challenge.

Can someone please explain why this code1 is incorrect and code2 is correct.

code1
public class StringArrayTest {
public static string[] generateStringArray()
{   
list <string> name= new list <string>();
integer i;
integer n=3;
for(i=0;i<=n;i++){
    name.add('Test' +' '+ i);
system.debug(name[i]);

}
return name;
    }
}

code2

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