Quick win: greet by name
Write Apex · Beginner · 5 min · Runs in your Salesforce Developer org · Free
Scenario. A welcome message needs to work for any name, so the name has to come from the input, not be typed in.
Join text and a variable with + to build a greeting for any name.
What you will learn
+ joins text together. Text in Apex sits inside single quotes.
String farewell(String name) {
return 'Bye, ' + name + '.';
}
farewell('Ana') gives Bye, Ana.. Watch the spaces: the space after the comma lives inside the quotes.
The task
Write a method greet(String name) that returns Hello, followed by the name and !.
Examples:
greet('Ana')→Hello, Ana!greet('Jo Li')→Hello, Jo Li!
Requirements:
- Return
'Hello, ' + name + '!'. - Keep exactly one space after the comma and no space before
!. - Keep the method's name and its String input.
Starter code
String greet(String name) {
return null;
}
System.debug(greet('Ana'));
System.debug(greet('Sam'));
When it passes
`greet` returns `Hello, <name>!` for every name the grader tries.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.