Basic counter using Shortcuts & Data Jar
In this challenge I was given the task of creating a counter using Data Jar and Shortcuts. It would do five basic functions(create value, delete value, view value, increment value, decrement value.). I watched lot of youtube videos and got a lot of help but in the end was able to make the program work. the rest of the blog post details what I did and what I learned while making it. The shortcut gives you five options of what you want to do to a value in Data Jar.
In this Shortcut you can view a value, increment a value, decrement a value, create a value and delete a value. Let’s start with incrementing and decrementing values. to do this I first had to first get the keys for people to choose from. I had to learn not to use a list because keys can be added and deleted so I had to use the get keys action then use a menu with the information from the get keys action. They then choose what they want to change. Then the computer gets the value by putting counter.(the dictionary name) then the variable of what they chose from the list. Then I used the value that I got previously as the variable in a calculator action with +/- one depending if you were incrementing or decrementing the value. Finally I would set the value by using counter. the item that was chosen before to the calculation result then a show result to show what the value was equal to.
The only thing that was different when decrementing a value was I couldn’t let the value be negative. To work around this I used an if statement and said if the calculation result was greater than or equal to 0 it would work fine otherwise a alert will pop up saying “You cannot set the below zero” and the program will end there. From this part I learned how to get a value using the dictionary name dot whatever value you want. this allowed me to use that as a variable to use in the calculator and show what the value is equal to. I also learned how to deal with the amount of values and names of values changing by getting it directly from the source instead of pre-coding it. The final thing I learned from this was that a value shouldn’t go below zero and I can’t trust the user to make sure the counter doesn’t go below zero so I have to control it myself to make sure the counter cannot go below zero which is just a good thing a can carry throughout all of my programing.
The next thing was creating a counter. First the program asks for what you want to name the new value then creates that value with the name you put in. Next the program sets the value of the new value to 0 . Finally the program shows the result by saying that the counter was created. I learned that you you can set a value that doesn’t exist and the program will make the variable which was tripping me up previously and since this was technically the first one I coded I also had to learn that you have to put the dictionary name dot whatever you are accessing which I had been confused about before.
Next is removing a counter, First I had to get the keys from a dictionary(this was the second one I did so I hadn’t done this before) which I was super confused on. I kept on getting confused because it is written like this “Get keys in ____ at _____” I was always getting stuck on this because in the first slot you are supposed to put dictionary then after you put the name of the dictionary you are getting it from. I kept putting the dictionary I was getting it from first then not knowing what to put in the second blank slot. Once I understood that I was able to use that action fine throughout the rest of my coding. Going back to the program, the person has to choose which value they want to delete. After they choose it gives you a warning making sure you want to delete the value. If yes it uses the delete value action which was the one action I understood from the get-go otherwise if you say no it just does nothing. From this I learned how to use the get keys action which I would need for the rest of the programmed some more controlling just making sure if you pressed the wrong button you wouldn’t have to delete a value.
The final one is viewing what a value is equal to. This was the last thing i did so I knew how to use everything. First is getting the keys from the dictionary then using that list in a list to pick from. Finnaly get the value then just a show result with the chosen item and the value we got from the previous action.
Overall I learned how to use most of the Data Jar actions in Shortcuts and how they can be utilized in different ways to be very helpful. I also learned more about dictionaries and how they are used in code. Dictionaries are a collection of data in key value pairs, and in this program is used to store multiple counters with numbers attached to them. I now can do more complex things with shortcuts now that I know how to use Data Jar and the actions that go with it in shortcuts.
The Shortcut
Part of Summer Challenges.