Unit 4 Variables, Conditions, and Functions
How is a Local Variable created?
var used inside an onEvent()
What are examples of logic errors?
Writing if-else-if statements in the wrong order Updating the property of the wrong element
What are specific debugging skills?
1)Slow down code (2) Use console.log to output (3) Use Watchers to see your variables change values
var x = 2; var y = 3; x = y; y = 5; x = 10; y = 4; console.log(x);
10
var x = 5; x = x+1; x = x+4; x = x+9; console.log(x);
19
How does a Local variable work?
Temporary. Can be used only in the part of the code where it was created, like inside an onEvent(). Deleted once the onEvent() is done running.
Predict the information that is being stored in variables for the thermostat app?
The information that is being stored in the temperature in Fahrenheit.
How does the Fahrenheit app work?
The initial temperature is set to 70. When the down button is clicked it will subtract the temperature at the given time by 2. Then that number will be applied to add an F. The numbers will actually show on screen and play a sound. Vice versa.
What is the biggest issue with local variables?
accidentally using var inside of an onEvent() or function.
What is an expression?
Any valid unit of code that resolves to a value.
What are the benefits of writing a song in Style 2?
Can save time repeating lyrics and can be easily repeatable
How to debug logic errors?
Test your code
What will the value of score be at the end of the program? Note: <- is used to represent a left facing arrow var score score <- 3 score <- score + 1 score <- "The score is: " + score
The score is: 4
var x = 5; x = x+1; x = x+4; x = x+9; if(x<10){ console.log("Less than 10"); } else if ((x<20)){ console.log("Less than 20"); } else if ((x<30)){ console.log("Less than 30"); } else { console.log("More than 30"); }
"Less than 20"
What is the pattern of writing these programs?
Add UI elements to the screen. Give the UI elements meaningful IDs. Add event handlers to those elements.
Imagine you want to make a decision about what to wear to an event. Name two pieces of information you'd want. How would you use them in your decision?
The occasion The weather
What is a function?
a named group of programming instructions. Also referred to as a "procedure".
Which of the following COULD NOT possibly be the output? a <- RANDOM(1,10) b <- RANDOM(10,20) DISPLAY(a) DISPLAY(a) DISPLAY(b) DISPLAY(b)
10 5 10 5
What are logical operators?
Boolean values are a type of information, so they can also be evaluated in a Boolean expression using logical operators.
What is the difference between a boolean value and boolean expression?
Boolean values are true or false and boolean expressions evaluates to either true or false
What does "if" refer to?
Means there is a conditional statement that decides what pieces of code to run. The app does something "if" a boolean expression evaluates to true.
What does "when" refer to?
Means there is an onEvent to respond to user input. The app does something "when" the user clicks.
How does a Global variable work?
Permanent. Can be used anywhere in your code.
What is a syntax error?
Your code doesn't follow the rules of the programming language
What is a logic error?
Your code follows the rules of the programming language but doesn't do what you intend
What is a function call?
a command that executes the code within a function
What are the Logical Operator functions?
&& AND || OR ! NOT
In your own words describe the benefits of creating functions in your code?
Creating functions in my own code allows for complex repeated actions to have a name and then can be easily repeated again. Otherwise, the code would be robust and take longer than it should program and not have to take up that much space if specific functions can be repeated which saves a compound number of bits.
score <- 4 lives <- 3 If lives=0 Display "You Lose!" else if score >= 5 AND lives = 3 Display "Perfect Game!" Else Display "You Win!"
You Win!
How to debug syntax errors?
Check warnings and errors
What info do I need to know about score* my lives is greater than 10?
Score and Lives
What are booleans?
Value that is true or false
What info do you need to know going to the movies?
Showtime, The length of the drive, The length of the movie
What can be stored in a variable?
Variables can store anything that is important to hold or to be called back upon.
What do comparison operators do?
indicate a Boolean expression
number <- INPUT() IF (number >= 10) { IF (number <= 20) { DISPLAY("MEDIUM") } ELSE { DISPLAY("HOT") } } ELSE { DISPLAY("COLD") }
5
What is a data type?
All values in a programming language have a "type" - such as a Number, Boolean, or String - that dictates how the computer will interpret it. For example 7+5 is interpreted differently from "7"+"5"
Explain in your own words the process of creating and updating a variable. How does the Counter Pattern with Event work?
Creating a variable and updating a variable is put on the top to allow for a call back to the variable in the program. Variables allow for changes in the program and are important for this reason. The Counter Pattern Event works as an event program so the initial sequential program gets disrupted and a change in an event happens.
day_of_week = "Saturday" day_of_week = "Monday' IF day_of_week= "Saturday" OR day_of_week= "Sunday" weekend ELSE weekday PROCEDURE weekday DISPLAY "School day" PROCEDURE weekend DISPLAY "Day off"
School day
When creating an if-else-if statement you should always make your first condition the most specific. Write a short paragraph responding to the questions below.
The most specific case first means the most selective part of the if-else-if statement or the case that will help determine the cases of the others. It's important to put the most specific case first because the computer runs code from top to bottom and if the most selective part is anywhere but the top then this will create massive bugs
What is the difference between an if-statement, an if-else statement, and an if-else-if statement? How are they similar?
They are essentially the same thing but get more complicated as it goes on. An if-statement is causes something to happen if another happens. The else adds on to another contingency which will cause another thing to happen. The else if is a continuation of the if statement and is a more specific contingency.
What should you do when creating variables?
Use var only once. You don't need to create variables twice and this can cause errors. Create your variables at the top of your program. This keeps your code organized and easier to read for you and others. Create your variables outside any function or onEvent()bocks.
How is a Global variable created?
var used outside an onEvent()
What are strings?
Made of any characters Inside double quotes
Why is using a meaningful name for a variable important?
A meaningful variable is important for the coder to remember what the variable entails.
What is the process of debugging?
Describe, Hunt, Try, Document
What are numbers?
Made of digits 0 through 9
How does math.round work?
Math.Round rounds a long decimal number to the nearest tenth.
What can info be stored as?
Numbers, Strings, Booleans
How does getText() work?
This allows for a callback to an input that the user put in.
What are examples of syntax errors?
Writing a variable name in quotes Using a variable that doesn't exist
Can a computer evaluate an expression to something between true and false? Can you write an expression to deal with a "maybe" answer?
Yes the computer can evaluate an expression to something between true and false. A computer can't deal with a maybe answer. Chocolate cookies < vanilla cookies This will give an expression to deal with a "maybe" answer for humans because somebody might have a different opinion.
What is a variable?
a reference to a value or expression that can be used repeatedly throughout a program or a placeholder for a piece of info that can change
How does a baggy represent a variable?
The baggy represents a variable because it is assigned a value of 3. Variables are assigned values.