knowt logo

Unit 4: Variables, Conditionals, and Functions

  • Numbers

    • Made of the digits 0…9

    • No quotes

  • Strings

    • Made of any characters

    • Inside double quotes

  • Operators

    • Fancy name for +-*/

  • Expression

    • Combination of operators and values

    • Evaluates to a single value

  • Variables

    • Can hold at most 1 value

    • Name uses no quotes, includes no spaces, and must start with a letter

    • A reference to a value or expression that can be used repeatedly throughout a program

  • var

    • Creates a new variable

  • Numbers and strings are 2 different types of values

  • Expressions evaluate to a single new value

  • When variables are in the expression just make a copy, don’t change the actual variable

  • Variables are “assigned” a new value

  • Evaluate first, then assign

  • Old values are deleted forever

  • Assignment just moves information around. It does not “connect” variables

  • In Javascript, the assignment operator is written as “=”

  • Assignment Operator

    • Allows a program to change the value represented by a variable

  • Counter Pattern with Event

  • Variable with String Concatenation

  • Debugging: the process of finding and fixing problems in code

    • Describe the problem

      • What do you expect it to do?

      • What does it actually do?

      • Does it always happen?

    • Hunt for bugs

      • Are there warnings or errors?

      • What did you change most recently?

      • Explain you code to someone else

      • Look for code related to the problem

    • Try solutions

      • Make a small change

    • Document as you go

      • What have you learned?

      • What strategies did you use?

      • What questions do you have?

    • Slow down code with the speed slider

    • Use console.log to get output

    • Use Watchers to see your variables change values

    • Syntax Error

      • Your code doesn’t follow the rules of the programming language

        • Writing a variable name in quotes

        • Using a variable that doesn’t exist

      • Check warnings and errors

    • Logic Error

      • Your code follows the rules of the programming language but doesn’t do what you intend

        • Writing if-else-if statements in the wrong order

        • Updating the property of the wrong element

      • Test your code

  • Create Variables Once, At the Top, Outside Functions or onEvent()

    • 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 you code organized and easier to read for you and others

    • Create your variables outside any function or onEvent() blocks

  • Global vs. Local Variables

    • There’s 2 types of variables, global and local, and so far we’ve only used global variables

    • Global

      • Permanent. Can be used anywhere in your code

      • var used outside an onEvent()

    • Local

      • 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

      • var used inside an onEvent()

    • Make sure that you’re not using var inside of an onEvent() or function

  • Boolean value

    • A data type that is either true or false

  • Boolean Expression

    • Evaluates to either true or false

  • Comparison operators

    • Indicate a Boolean expression

    • < less than

    • greater than

    • == equal to

    • <= less than or equal to

    • = greater than or equal to

    • != not equal to

  • Logical operators

    • Boolean values are a type of information, so they can be evaluated in a Boolean expression using logical operators

    • Both sides of the logical operator are reduced to a single Boolean value

    • && and

      • Both must be true for the Boolean expression to evaluate as true

    • || or

      • Either may be true for the Boolean expression to evaluate as true

    • != not

      • Opposite of the Boolean value

      • Not true: flase

      • Not false: true

    • If something is true and (&&) false, it evaluates to false

  • Single values are compared and result in a Boolean value (true or false)

  • A truth table is is used to evaluate the reduced Boolean expression to a single Boolean value

  • A decision is made with the single Boolean value

  • A flowchart illustrates the steps of making a decision with a Boolean expression

  • “when”

    • Means there is an onEvent to respond to user input. The app does something “when” the user clicks

  • “if”

    • 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

  • MOD

    • The remainder that is left after a number is divided by another number

    • A common usage is to determine if a number is even or odd. If you divide any number by 2 and there is no remainder, the number is even

    • You can use MOD to determine if a number is divisible by another number

  • Checking Multiple Conditions with If-Else-If

  • The updateScreen() Pattern

    • function

  • Function

    • A named group of programming instructions. Also referred to as a “procedure”

  • Function call

    • A command that executes the code within a function

ZH

Unit 4: Variables, Conditionals, and Functions

  • Numbers

    • Made of the digits 0…9

    • No quotes

  • Strings

    • Made of any characters

    • Inside double quotes

  • Operators

    • Fancy name for +-*/

  • Expression

    • Combination of operators and values

    • Evaluates to a single value

  • Variables

    • Can hold at most 1 value

    • Name uses no quotes, includes no spaces, and must start with a letter

    • A reference to a value or expression that can be used repeatedly throughout a program

  • var

    • Creates a new variable

  • Numbers and strings are 2 different types of values

  • Expressions evaluate to a single new value

  • When variables are in the expression just make a copy, don’t change the actual variable

  • Variables are “assigned” a new value

  • Evaluate first, then assign

  • Old values are deleted forever

  • Assignment just moves information around. It does not “connect” variables

  • In Javascript, the assignment operator is written as “=”

  • Assignment Operator

    • Allows a program to change the value represented by a variable

  • Counter Pattern with Event

  • Variable with String Concatenation

  • Debugging: the process of finding and fixing problems in code

    • Describe the problem

      • What do you expect it to do?

      • What does it actually do?

      • Does it always happen?

    • Hunt for bugs

      • Are there warnings or errors?

      • What did you change most recently?

      • Explain you code to someone else

      • Look for code related to the problem

    • Try solutions

      • Make a small change

    • Document as you go

      • What have you learned?

      • What strategies did you use?

      • What questions do you have?

    • Slow down code with the speed slider

    • Use console.log to get output

    • Use Watchers to see your variables change values

    • Syntax Error

      • Your code doesn’t follow the rules of the programming language

        • Writing a variable name in quotes

        • Using a variable that doesn’t exist

      • Check warnings and errors

    • Logic Error

      • Your code follows the rules of the programming language but doesn’t do what you intend

        • Writing if-else-if statements in the wrong order

        • Updating the property of the wrong element

      • Test your code

  • Create Variables Once, At the Top, Outside Functions or onEvent()

    • 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 you code organized and easier to read for you and others

    • Create your variables outside any function or onEvent() blocks

  • Global vs. Local Variables

    • There’s 2 types of variables, global and local, and so far we’ve only used global variables

    • Global

      • Permanent. Can be used anywhere in your code

      • var used outside an onEvent()

    • Local

      • 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

      • var used inside an onEvent()

    • Make sure that you’re not using var inside of an onEvent() or function

  • Boolean value

    • A data type that is either true or false

  • Boolean Expression

    • Evaluates to either true or false

  • Comparison operators

    • Indicate a Boolean expression

    • < less than

    • greater than

    • == equal to

    • <= less than or equal to

    • = greater than or equal to

    • != not equal to

  • Logical operators

    • Boolean values are a type of information, so they can be evaluated in a Boolean expression using logical operators

    • Both sides of the logical operator are reduced to a single Boolean value

    • && and

      • Both must be true for the Boolean expression to evaluate as true

    • || or

      • Either may be true for the Boolean expression to evaluate as true

    • != not

      • Opposite of the Boolean value

      • Not true: flase

      • Not false: true

    • If something is true and (&&) false, it evaluates to false

  • Single values are compared and result in a Boolean value (true or false)

  • A truth table is is used to evaluate the reduced Boolean expression to a single Boolean value

  • A decision is made with the single Boolean value

  • A flowchart illustrates the steps of making a decision with a Boolean expression

  • “when”

    • Means there is an onEvent to respond to user input. The app does something “when” the user clicks

  • “if”

    • 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

  • MOD

    • The remainder that is left after a number is divided by another number

    • A common usage is to determine if a number is even or odd. If you divide any number by 2 and there is no remainder, the number is even

    • You can use MOD to determine if a number is divisible by another number

  • Checking Multiple Conditions with If-Else-If

  • The updateScreen() Pattern

    • function

  • Function

    • A named group of programming instructions. Also referred to as a “procedure”

  • Function call

    • A command that executes the code within a function