Skip to content

Instantly share code, notes, and snippets.

@katelessardrad
Last active September 9, 2019 15:41
Show Gist options
  • Save katelessardrad/3fdbb559713dadc54d0cf32abc60cd47 to your computer and use it in GitHub Desktop.
Save katelessardrad/3fdbb559713dadc54d0cf32abc60cd47 to your computer and use it in GitHub Desktop.
Week 3 Homework
public with sharing class WeekThreeHomework {
public static void homeworkAssignmentMethod() {
//Read through the set-up below and then complete the code following the prompts. When you're done, make sure to compile (save) your work
//Open Execute Anonymous in the Developer Console and execute your code by typing in: WeekThreeHomework.homeworkAssignmentMethod();
//Read through the debug statements to make sure you're done your work correctly.
//************************************************************************************************************
// 1. Add two more whole numbers to this list using .add()
List<Integer> numberList = new List<Integer>{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
numberList.add (4);
numberList.add (11);
System.debug('This number should be 12: ' + numberList.size());
//Question on this one! I initially tried numberList.add (4,11); and my system debug returned 11 instead of 12. Why did this happen when they were in 1 .add?
//************************************************************************************************************
// 2. Create a new Lead and insert it in the database. (If you're stuck, look back at the WeekThreeClassExercises class for an example of creating a new SObject Record)
//You can give it any values you like, but remember that last name and Company are required fields (both are simple text fields.)
Lead a = new Lead();
a.FirstName = 'Lucy';
a.LastName = 'Dog';
a.Company = 'codeVengers';
a.Title = 'Kate Support';
insert a;
System.debug('We should see One DML was executed: ' + Limits.getDMLRows());
//************************************************************************************************************
//3. For the loop that is commented out below, update the while condition by replacing the ?? so that the loop runs 5 times
//delete the slashes so that the loop is no longer commented out and compile the class.
//Can you add a debug statement to print out the counter value every time it runs through the loop?
Integer counter = 0;
while (counter < 5) {
counter++; //without this line to increment counter, we'd have an infinite loop!
}
System.debug('Done with the loop, it ran: ' + counter + ' times.');
}
}
@katelessardrad
Copy link
Author

Swati- I added a question on this one (as a comment) for the first exercise... can you take a look? I'm wondering what the difference between my initial trial and where I ended up (with the correct debug result) is! Thank you :)

@swatimarda
Copy link

swatimarda commented Sep 9, 2019

Great Question Kate ! So the add method works in specific ways:

  • list.add(value) actually allows only one value at a time, the way you have it written now is correct!

  • there is a way to add multiple values to a list, we can definitely discuss in class

  • and answer to your question, there is another variation of the same add method as highlighted below. So basically when you write numberList.add (4,11), it adds 11 to the 4th index (5th position) on your list. Let me know if this makes sense, if not we can get on a call after our session tomorrow and I can explain :)

    add(index, listElement) : Inserts an element into the list at the specified index position.

@swatimarda
Copy link

Also noted:

  1. You would need to remove the '//' from line 43
  2. Also you might be missing a System.debug as mentioned on line 36

@katelessardrad
Copy link
Author

Thank you- that is helpful! The // gets added when I enter sometimes... do you know if there is a way to turn that off?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment