This is the main language. Extremely important, as such, Ill introduce a lot of concepts – some familiar- some not. They will all help you out for the exams and understanding. Each section has a clear concept to it and example problems that you can try your hand in!
Arrays are a collection of strings or numbers stored in one variable. For example,
artists = [“Rich Brian”, “Swae Lee”, “Tame Impala”];
fortunecookienumbers = [10, 23, 25, 18];
Click below to learn all about arrays!
for example we have a string variable called: places = “NewYork#London#Aruba#Dubai”;
We want to split places into each individual city.
city = places.split(“#”);
city is now equal to [“NewYork”, “London”, “Aruba”, “Dubai”].
p.s. city is now a 4 element array.
The for loop
The for loop is structured like this: for(i=0;i<10;i++)
for(i= (any number you want to start with);
i < (a number to compare i with, if its’ true the loop will execute, if false the loop will stop executing);
i += (how many times you want to increment i each time you complete one loop)
The for loop is important because it makes it possible to loop through many array values to output into a table or check for specific values inside arrays. Our exams will have arrays that have either 100-400 elements inside them. a for loop is efficient to cycle through each value in an array from position 0 onward to the end of the array.
More detail and fun practice problems will now follow.
The inner for loop (a loop inside a loop)
I promise this isn’t as difficult to grasp like the first time watching inception. It’s a (relatively) simple concept. There are really only two scenarios we will only ever need/write an inner loop.
One: for outputting data cells into a specific row. (what we will learn now)
Two: for checking any values of data cells if they match with a pre-defined array. (more on this later..after I go over if statements and parallel arrays)
if statements are generally the easiest concept to grasp. We do if statements in our own heads every day. for example:
if raining carry umbrella else leave umbrella at home.
if statements are important in TP. They are the “logic” in our program. I find it better for me to do less talking and you to see its’ application. so heres plenty of if statements when you click on the button.
The .indexOf() function is a helpful tool for if statements. It takes the argument inside the (), and sees if the word contains all of the letters of the argument. if it does, the indexOf function will return a number position of the first occurence of that letter.
location = “Houston,Texas”;
if(location.indexOf(“Texas”) > -1)
this if returns true.
The while loop is very similar to the for loop. In this class there really is only one use of the while loop.
Let’s say we have money = “1,242,123”;
And we want to remove the commas. so money = can look like “1242123”.
This is where the while loop comes in handy. Click to learn all about it!