JavaScript Lists lesson!
The fundamentals of making lists!
Hello! Welcome to my silly little lesson about lists!
Now, you are probably wondering, “Hey! What is a list?” I was just getting into that, so maybe, shut up and listen.
What is a list?
A list is a data structure that is used to store an ordered collection of items. Now, for a question none of you are probably asking, “Hey! How often are Lists used?” And my friend, they are used alot. In fact, they are one of the most common data types across all the different coding languages! Long story short, you will need to use them, so learn to like em’!
Lists 101 pt.1
-
How to make a list Making a list is simple! What you want to know first is “aList[i]”.
-
“aList[i]” can be broken into two parts: aList, and the [i].
-
“aList” is the name of your list. So, if you want to make a list of, say, your favorite foods, you would name your list “favorite_foods”. It’s that simple!
-
Next is the whatever this thing is —–> [i]. That right there? That’s where uou put the stuff you want to list! So, again, with the favorite foods example, you will replace the [i] with [‘pizza’, ‘ice_cream’, ‘hamburgers’];
-
Remember to always use underscores when there is a space, or as an alternative, instead of formatting it like this: “let favorite_foods”, you can instead use, “let favoriteFoods”
-
Now, putting this all togeather, it should look like this!: let favorite_foods = [‘pizza’, ‘ice_cream’, ‘hamburgers’];
//Here is an example:
let aList = (pizza, ice_cream, hamburgers)
// Or, you can use some silly capitalization instead of underscore, like this:
let aList = (pizza, iceCream, hamburgers)
Popcorn Hack!!!
That’s right! It’s time for a popcorn hack!!! So, do you want to make a list, but you feel like you might want to add stuff later, and don’t feel like going back into the code to change it? If yes, this hack is for you! All you have to do is:
%%js
let aList = ['Food', 'Giraffes', 'Cats']
let user_input = ("Enter an item you want (or Enter to quit): ")
do {
userInput = prompt("Enter an item you want (or just press Enter to quit):");
if (userInput !== "") {
aList.push(userInput);
}
} while (userInput !== "");
console.log(aList)
<IPython.core.display.Javascript object>
It’s that simple! Now, what is happening? I’ll break it down. We don’t need to talk more about the aList and brackets, so lets move on the the “USER_INPUT ← (“Enter an item you want (or Enter to quit): “)” What is this? It’s simple. It is just telling the computer how to put an item into the list. Translated in to normal language it says, “when USER presses Enter, put [item] into aList.
Next! what is,
let userInput;
do {
userInput = prompt("Enter an item you want (or press Enter to quit):");
if (userInput) {
aList.push(userInput);
}
} while (userInput !== "");
This is telling the computer what to do when Enter is pressed, bacically saying to keep on putting the characters into the list until Enter.
Now, how do you apply this to your game? It’s simple! If your game has many different side quests and main quests going on at the same time, or you have quests that take a wile, your players might need a reminder of what to do or where to go! You can use lists to make a task log or task book just in case your player forgets what to do! As homework, make a list that displays 3 quests you want your player to do. GET TO IT!