You need to sign in to do that
Don't have an account?
Squire Q Kershner
Not sure why my class won't pass evaluation
Good afternoon,
I can't seem to figure out why my class for the "Getting Started with Apex: Unit 1" won't pass evaluation. The class appears to work as expected when I run via the Execute Anonymous window, but the checker fails.
Here is my class:
public class StringArrayTest{
public static void generateStringArray(integer q){
list<string> test = new list<string>();
for(integer i=0; i < q;i++){
test.add('Test '+i);
system.debug(test[i]);
}
}
}
When I run StringArrayTest.generateStringArray(9); in the EAW, I get the return I expected.
Thoughts?
I can't seem to figure out why my class for the "Getting Started with Apex: Unit 1" won't pass evaluation. The class appears to work as expected when I run via the Execute Anonymous window, but the checker fails.
Here is my class:
public class StringArrayTest{
public static void generateStringArray(integer q){
list<string> test = new list<string>();
for(integer i=0; i < q;i++){
test.add('Test '+i);
system.debug(test[i]);
}
}
}
When I run StringArrayTest.generateStringArray(9); in the EAW, I get the return I expected.
Thoughts?
Looking at your code I found two changes that need to be done:
1) Insted of Void you will have to use List<String> return type, because Void method don't return values.
2) system.debug(test[i]); won't work in your case, as return should be List/Array of String.
Try this :
public class StringArrayTest{
public static List<string> generateStringArray(integer q){
list<string> test = new list<string>();
for(integer i=0; i < q;i++){
test.add('Test '+i);
}
return(test);
}
}
Mark as best answer if it works for you.
All Answers
Looking at your code I found two changes that need to be done:
1) Insted of Void you will have to use List<String> return type, because Void method don't return values.
2) system.debug(test[i]); won't work in your case, as return should be List/Array of String.
Try this :
public class StringArrayTest{
public static List<string> generateStringArray(integer q){
list<string> test = new list<string>();
for(integer i=0; i < q;i++){
test.add('Test '+i);
}
return(test);
}
}
Mark as best answer if it works for you.
Mine: public static void generateStringArray(integer q){
Yours: public static List<string> generateStringArray(integer q){
So I understand that a VOID method can't return anything, so that made sense. But why does adding the datatype (List<string>) make it work? I have a guess, but would like to hear it from the pros first.
- Output to log
- Returning a value
In your solution you did output to log. That's fine. And that's what you were seeing in execute anonymous. But the way your wrote your method, you weren't passing any data back to the thing that called it. That's what return is for.The requirement was that the method needed to return a value, in other words, it needed to be able to pass data back to the code that called it.
As such, the challenge/assessment was written in such a way as to expect the return statement. I expect the assessment writer was invoking the method, and then testing whether the correct data was returned. Something like this:
List<String> stringsToTest = StringArrayTest.generateStringArray(10);
System.assert(10,stringsToTest.size());
With your original code, this test would either throw an exception (because you can't pass 'void' into List<String>) or it would always fail.
Does that help?
(By the way, did "return(test);" work? Because normally correct syntax would be "return test;.)