Unit 1: Day 7
Tasks
7.1 How Code and Variables Work in Memory
- If you have access to actual hardware pieces, use the hard drive and RAM chips as references.
- RAM is like a cupboard full of shelves
- Each shelf has a specific address
- Creating a variable names a shelf
- Some languages (but not ours) have TYPES of variables. This makes memory management more efficient but makes coding more complex.
- When you reference a name of a variable, it is going to see what is in that shelf:
- let x; //this names the shelf in memory x;
- x = 15; //fills the shelf with the number 15
- print(x); //gets whatever is in shelf x
- x = x + 10; // gets what is in shelf x, adds 10 to that number, resaves it in shelf x
- if (x===25){ } // checks to see what is in shelf x
7.2 Variables Example - Ticket Sales
- Before beginning, talk about empty variables. Demo the error if you try to print an empty variable and print an empty variable with math.
- let empty1;
- let empty2;
- function draw() {
- text (empty1, 50,50);
- text ((empty2*5), 50,50);
- }
- Introduces window.prompt()
7.3 Discuss the Following AP Concepts
- AAP-1.A.1: A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
- AAP-1.A.2: Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.
- AAP-1.A.3: Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
- AAP-1.A.4: Some values are better suited to representation using one type of data rather than another.
- AAP-1.A.4: Some values are better suited to representation using one type of data rather than another.
- AAP-1.B.2: The exam reference sheet provides the “←” operator to use for assignment.
- AAP-1.B.3: The value stored in a variable will be the most recent value assigned.
Unit 1 - Example 7 - Variables
let event = "Big Fish";
let preSales;
let daySales;
let prePrice = 15;
let dayPrice = 20;
let totalSales;
function setup() {
let sketch = createCanvas(600, 400);
sketch.parent("mycanvas");
background(150,50,50);
textSize(18);
preSales = 200;
event = window.prompt("What is the name of this year's event?")
event = event +" - " + window.prompt("What is the year?")
daySales = window.prompt("How many tix did we sell today?");
// window.alert("Total tickets sold is " + (preSales + daySales) );
}
function draw() {
background(150,250,250);
textSize(40);
text(event, 50,50);
textSize(18);
text("Presales: " + preSales, 50, 100);
text("Price $" + prePrice, 200, 100);
text("Today sales: " + daySales, 50, 125);
text("Price $" + dayPrice, 200, 125);
totalSales = preSales * prePrice + daySales * dayPrice;
text("Our total is $" + totalSales,50,200);
}
} function keyPressed(){
daySales++;
}
Continue to Unit 1: Day 8 »