Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 126 additions & 53 deletions force-app/main/default/classes/FlowControlLoopsCollections.cls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* in flow control, loops, and collections. By mastering these concepts, developers will be able to write more
* efficient and effective code in their Salesforce projects.
*
* @author Your Name
* @author McKay Howell
*/

public with sharing class FlowControlLoopsCollections {
Expand All @@ -29,7 +29,10 @@
* @return "Hello World!" if x greater than y, otherwise return null.
*/
public static String helloWorld(Integer x, Integer y) {
return null; // Replace null with the variable you used to store the result
if (x == null || y == null || x <= y) {
return null;
}
return 'Hello World!'; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -42,7 +45,7 @@
* @return true if the person is eligible to vote, false otherwise.
*/
public static Boolean votingEligibility(Integer age) {
return null; // Replace null with the variable you used to store the result
return (age >= 18); // Replace null with the variable you used to store the result
}

/**
Expand All @@ -55,10 +58,11 @@
* @return The maximum of the two numbers, or null if either number is null.
*/
public static Integer findMax(Integer num1, Integer num2) {
// Initialize largestNum as null
Integer largestNum = null;

return null; // Replace null with the variable you used to store the result
if ( num1 == null || num2 == null) {
return null;
}
Integer largestNum = Math.max(num1, num2);
return largestNum; // Replace null with the variable you used to store the result
}


Expand All @@ -71,7 +75,15 @@
* @return A string indicating whether the number is "Positive", "Negative", or "Zero", or null if the number is null.
*/
public static String checkNumber(Integer a) {
return null; // Replace null with the variable you used to store the result
if (a == null) {
return null;
} else if (a > 0) {
return 'Positive';
} else if (a < 0) {
return 'Negative';
} else {
return 'Zero';
}
}

/**
Expand All @@ -83,7 +95,13 @@
* @return A string indicating whether the number is "Even" or "Odd", or null if the number is null.
*/
public static String checkEvenOdd(Integer a) {
return null; // Replace null with the variable you used to store the result
if(a == null) {
return null;
} else if (Math.mod(a, 2) == 0) {
return 'Even';
} else {
return 'Odd';
}
}

/**
Expand All @@ -94,7 +112,11 @@
* @return A string indicating whether the input is "Empty", "Null", or "Contains Text".
*/
public static String checkString(String a) {
return null; // Replace null with the variable you used to store the result
if (String.isBlank(a)) {
return a == null ? 'Null' : 'Empty';
} else {
return 'Contains Text';
}
}

/**
Expand All @@ -110,7 +132,17 @@
* @return The grade for the given score.
*/
public static String determineGrade(Integer score) {
return null; // Replace null with the variable you used to store the result
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else if (score >= 60) {
return 'D';
} else {
return 'F';
}
}


Expand All @@ -122,13 +154,11 @@
* @return The sum of all integers up to the limit.
*/
public static Integer sumUpToLimit(Integer intLimit) {
/*
for (){

}
*/

return null; // Replace null with the variable you used to store the result
Integer sum = 0;
for (Integer index = 1; index <= intLimit; index++){
sum += index;
}
return sum; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -139,15 +169,13 @@
*/
public static String returnHelloWorld() {
// Initialize the result String

String result = '';
// Use a for loop to append 'Hello World!;' to the result string three times
/*
for (){

}
*/
for (Integer index = 1; index <= 3; index++){
result += 'Hello World!; ';
}

return null; // Replace null with the variable you used to store the result
return result; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -162,17 +190,19 @@
*/
public static String repeatString(String inputString, Integer repeatCount) {
// Initialize the result String
String result = '';

// Use a for loop to append the inputString to the result string repeatCount times
/*
for () {

for (Integer index = 1; index <= repeatCount; index++) {
result += inputString;
// If it is not the last iteration, add a semicolon to separate the strings
if (index != repeatCount) {
result += '; ';
}
}
*/

// Return the final result string
return null; // Replace null with the variable you used to store the result
return result; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -183,14 +213,14 @@
*/
public static List<Integer> createAndPopulateList() {
// Create a new list of integers

List<Integer> intList = new List<Integer>{1, 2, 3};
// Add the integers 1, 2, and 3 to the list
// add 1
// add 2
// add 3

// Return the populated list
return null; // Replace null with the variable you used to store the result
return intList; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -203,10 +233,11 @@
// Create a new list of integers and add numbers 1 to 5
List<Integer> numberList = new List<Integer>{1, 2, 3, 4, 5}; //DO NOT CHANGE

// Remove the 3rd element (number 3) from the list
// Remove the 3rd element (number 3) from the list; index values start with 0, so the third element has index 2
numberList.remove(2);

// Return the updated list
return null; // Replace null with the variable you used to store the result
return numberList; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -218,14 +249,13 @@
*/
public static List<Integer> createIntegerList(Integer n) {
// Create a new list of integers

/*
for() {
//add integer to the list
List<Integer> intList = new List<Integer>();

for(Integer index = 1; index <= n; index++){
intList.add(index);
}
*/

return null; // Replace null with the variable you used to store the result
return intList; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -237,12 +267,9 @@
* @return A Set of unique strings.
*/
public static Set<String> createStringSet(List<String> inputList) {
// Create a new Set of strings
Set<String> stringSet = new Set<String>();

//add strings to the set

return null; // Replace null with the variable you used to store the result
// Create a new Set of strings with values initialized from the inputList (deduplicated)
Set<String> stringSet = new Set<String>(inputList);
return stringSet; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -254,11 +281,19 @@
public static Integer sumPositiveIntegers() {
List<Integer> numbers = new List<Integer>{-1, 2, -3, 4, -5, 6}; //DO NOT CHANGE

Integer sum = 0;
// Loop through the list of integers
for(Integer num : numbers){
// if the number is negative skip this iteration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job using continue to skip negative numbers! This demonstrates you understand jump statements well.

I love that you're using a for-each loop here - it's cleaner and more readable than a traditional for loop when you don't need the index.

if(num < 0){
continue;
}
sum += num;
}



return null; // Replace null with the variable you used to store the result
return sum; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -271,11 +306,18 @@
*/
public static Integer findWordInList(String wordToFind, List<String> words) {
// The variable to store the index of the word
Integer index = -1;

// Loop through the list of words
for (String word : words){
// If the current word is the word to find, exit the loop
if(word.equals(wordToFind)){
index = words.indexOf(word);
break;
}
}

return null; // Replace null with the variable you used to store the result
return index; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -289,7 +331,15 @@
// The list of money in the wallet, represented in dollars and cents
List<Decimal> moneyInWallet = new List<Decimal>{0.50, 10, 3.84, 24.60, 9.08, 50, 4.90}; //DO NOT CHANGE

return null; // Replace null with the variable you used to store the result
final Decimal STOP_VALUE = 40.0;

Check warning

Code scanning / PMD

The final local variable name 'STOP_VALUE' doesn't match '[a-z][a-zA-Z0-9]*' Warning

The final local variable name 'STOP_VALUE' doesn't match '[a-z][a-zA-Z0-9]*'
Decimal total = 0.0;
for (Decimal money : moneyInWallet){
total += money;
if(total > STOP_VALUE){
break;
}
}
return total; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -300,15 +350,21 @@
*/
public static Map<String, Integer> addItemsToMap() {
// Initialize an empty Map
Map<String, Integer> fruits = new Map<String,Integer>();

// Add key-value pairs to the Map
// add Apples => 5
fruits.put('Apples', 5);
// add Oranges => 10
fruits.put('Oranges', 10);
// add Bananas => 15
fruits.put('Bananas', 15);
// add Pears => 20
fruits.put('Pears', 20);
// add Grapes => 25
fruits.put('Grapes', 25);

return null; // Replace null with the variable you used to store the result
return fruits; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -322,17 +378,23 @@
*/
public static Integer getSalary(String employeeName) {
// Initialize a Map
Map<String, Integer> employees = new Map<String, Integer>();

// Add key-value pairs to the Map
// add John Doe => 50000
employees.put('John Doe', 50000);
// add Jane Smith => 60000
employees.put('Jane Smith', 60000);
// add Sam Brown => 55000
employees.put('Sam Brown', 55000);
// add Alice Johnson => 65000
employees.put('Alice Johnson', 65000);

// Get the salary of the employee
Integer salary = employees.get(employeeName);

// Return the salary of the employee, or null if the employee is not found in the Map
return null; // Replace null with the variable you used to store the result
return salary; // Replace null with the variable you used to store the result
}

/**
Expand All @@ -349,20 +411,31 @@
// Initialize a Map
Map<String, Integer> employeeSalaries = new Map<String, Integer>();

// Add key-value pairs to the Map
// add John Doe => 50000
employeeSalaries.put('John Doe', 50000);
// add Jane Smith => 60000
employeeSalaries.put('Jane Smith', 60000);
// add Sam Brown => 55000
employeeSalaries.put('Sam Brown', 55000);
// add Alice Johnson => 65000
employeeSalaries.put('Alice Johnson', 65000);

// Initialize a list to store the names of high paid employees
List<String> highPaidEmployees = new List<String>();

final Integer HIGH_SALARY = 55000;

Check warning

Code scanning / PMD

The final local variable name 'HIGH_SALARY' doesn't match '[a-z][a-zA-Z0-9]*' Warning

The final local variable name 'HIGH_SALARY' doesn't match '[a-z][a-zA-Z0-9]*'
// Iterate over the Map using a for loop
for(String name : employeeSalaries.keySet()){
// Check if the salary of the employee is more than 55000
Integer salary = employeeSalaries.get(name);
if(salary > HIGH_SALARY){
// Add the employee to the list of high paid employees
highPaidEmployees.add(name);
}
}

// Return the list of high paid employees
return null; // Replace null with the variable you used to store the result
return highPaidEmployees; // Replace null with the variable you used to store the result
}

}
Loading