diff --git a/force-app/main/default/classes/FlowControlLoopsCollections.cls b/force-app/main/default/classes/FlowControlLoopsCollections.cls index eae7417..469725f 100644 --- a/force-app/main/default/classes/FlowControlLoopsCollections.cls +++ b/force-app/main/default/classes/FlowControlLoopsCollections.cls @@ -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 { @@ -29,7 +29,10 @@ public with sharing class FlowControlLoopsCollections { * @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 } /** @@ -42,7 +45,7 @@ public with sharing class FlowControlLoopsCollections { * @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 } /** @@ -55,10 +58,11 @@ public with sharing class FlowControlLoopsCollections { * @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 } @@ -71,7 +75,15 @@ public with sharing class FlowControlLoopsCollections { * @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'; + } } /** @@ -83,7 +95,13 @@ public with sharing class FlowControlLoopsCollections { * @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'; + } } /** @@ -94,7 +112,11 @@ public with sharing class FlowControlLoopsCollections { * @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'; + } } /** @@ -110,7 +132,17 @@ public with sharing class FlowControlLoopsCollections { * @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'; + } } @@ -122,13 +154,11 @@ public with sharing class FlowControlLoopsCollections { * @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 } /** @@ -139,15 +169,13 @@ public with sharing class FlowControlLoopsCollections { */ 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 } /** @@ -162,17 +190,19 @@ public with sharing class FlowControlLoopsCollections { */ 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 } /** @@ -183,14 +213,14 @@ public with sharing class FlowControlLoopsCollections { */ public static List createAndPopulateList() { // Create a new list of integers - + List intList = new List{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 } /** @@ -203,10 +233,11 @@ public with sharing class FlowControlLoopsCollections { // Create a new list of integers and add numbers 1 to 5 List numberList = new List{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 } /** @@ -218,14 +249,13 @@ public with sharing class FlowControlLoopsCollections { */ public static List createIntegerList(Integer n) { // Create a new list of integers - - /* - for() { - //add integer to the list + List intList = new List(); + + 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 } /** @@ -237,12 +267,9 @@ public with sharing class FlowControlLoopsCollections { * @return A Set of unique strings. */ public static Set createStringSet(List inputList) { - // Create a new Set of strings - Set stringSet = new Set(); - - //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 stringSet = new Set(inputList); + return stringSet; // Replace null with the variable you used to store the result } /** @@ -254,11 +281,19 @@ public with sharing class FlowControlLoopsCollections { public static Integer sumPositiveIntegers() { List numbers = new List{-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 + 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 } /** @@ -271,11 +306,18 @@ public with sharing class FlowControlLoopsCollections { */ public static Integer findWordInList(String wordToFind, List 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 } /** @@ -289,7 +331,15 @@ public with sharing class FlowControlLoopsCollections { // The list of money in the wallet, represented in dollars and cents List moneyInWallet = new List{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; + 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 } /** @@ -300,15 +350,21 @@ public with sharing class FlowControlLoopsCollections { */ public static Map addItemsToMap() { // Initialize an empty Map + Map fruits = new Map(); // 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 } /** @@ -322,17 +378,23 @@ public with sharing class FlowControlLoopsCollections { */ public static Integer getSalary(String employeeName) { // Initialize a Map + Map employees = new Map(); // 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 } /** @@ -349,20 +411,31 @@ public with sharing class FlowControlLoopsCollections { // Initialize a Map Map employeeSalaries = new Map(); - // 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 highPaidEmployees = new List(); + final Integer HIGH_SALARY = 55000; // 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 } } \ No newline at end of file