Skip to content

Latest commit

 

History

History
158 lines (130 loc) · 5.29 KB

File metadata and controls

158 lines (130 loc) · 5.29 KB

Class 7: Programming w/JavaScript

Resources

Some Notes

Control Flow

  • Control Flow = the order in which the cpu executes statements in a script.
    • Code is run in order from first line to last line unless there are structures that change control flow, such as conditionals and loops.
  • A typical script in JS or PHP includes many control structures, including conditionals, loops and functions. Parts of a script may also be set to execute when events occur.

JavaScript Functions

  • A JS function is a block of code designed to perform a particular task.
  • Functions are executed when something "calls it"
    • // Function to compute the product of p1 and p2 function myFunction(p1, p2) { return p1 * p2; }
  • For a procedure to qualify as a funtion, it should take some input and return an output where there is some obvious relationship between the input and output.

JavaScript Function Syntax

  • JS functions are defined with the function keyword, followed by a name, followed by parentheses ().
  • Function names follow same rules as variables (contains letters, digits, underscores, and dollar signs).
  • The code to be executed by the function is placed inside curly brackets {}.
  • function name(parameter1, parameter2, parameter3) { // code to be executed }

Function Return

  • When JS reaches a return statement, the function will stop executing.
  • If the function was called from a statement, JS will return to execute the code after the invoking statement.
  • Functions often compute a return value. The return value is "returned" back to the "caller".
  • let x = myFunction(4, 3); // Function is called, return value will end up in x

function myFunction(a, b) { return a * b; // Function returns the product of a and b }

  • With functions you can define the code once and use it many times.
    • You can use the same code many times with different arguments to produce different results.
      • function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } document.getElementById("demo").innerHTML = toCelsius(77);
  • The () Operator invokes the function
    • function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } document.getElementById("demo").innerHTML = toCelsius;
  • Functions can be use as Variable values
    • let x = toCelsius(77); let text = "The temperature is " + x + " Celsius";

Local Variables

  • Variables declared within a JS function become LOCAL to the function.
    • Local variables can only be accessed from within the function.
    • // code here can NOT use carName

function myFunction() { let carName = "Volvo"; // code here CAN use carName }

// code here can NOT use carName

JavaScript Operators

  • The assignment operator (=) assigns a value to a variable.
    • let x = 10;
  • The addition operator (+) adds numbers.
    • let x = 5; let y = 2; let z = x + y;
  • The multiplication operator (*) multiplies numbers.
    • let x = 5; let y = 2; let z = x * y;

Types of JS Operators

    1. Arithmetic Operators
    • Additon = +
    • Subtraction = -
    • Multiplication = *
    • Exponentiation = **
    • Division = /
    • Modulus (Division Reminder) = %
    • Increment = ++
    • Decrement = --
    1. Assignment Operators
    • += example (x += y)=(x= x + y)
    • -= example (x -= y)=(x= x - y)
    • *= example (x *= y)=(x= x * y)
    • /* example (x /= y)=(x= x / y)
    • %= example (x %= y)=(x= x % y)
    • **= example (x **= y)=(x= x ** y)
    1. Comparison Operators
    • == eqaul to
    • === equal value and equal type
    • != not equal
    • !== not equal value or not equal type
    • greater than

    • < less than
    • = greater than or equal to

    • <= less than or equal to
    • ? ternary operator
    1. Logical Operators
    • && logical and
    • || logical or
    • ! logical not
    1. Conditional Operators
    1. Type Operators
    • typeof Returns the type of a variable
    • instanceof Returns true if an object is an instance of an object type

Replit Practice

// declaring a variable called theName // assigning it the value of the result of prompting // "What is your name?"

let favColor = "yellow" let userGuess = prompt("What is my fav color?"); userGuess = userGuess.toLowerCase(); console.log("Now user guess is: " + userGuess); // console.log("User guess is: " + userGuess); if (userGuess == favColor){ console.log("You are right."); } else { console.log("You are wrong."); } // let userName = prompt("What is your name?");

if (userName == "ryan") { console.log("Hola compa, hear any good chisme on the cartel lately?"); } else { console.log("Buenos Dias, Bolivia") } console.log("Welcome to Bolivia " + userName); //

Things I Would Like to Know More About

Maybe in time, I will be able to memorize most of these operators, but as of right now there are soooooo many. I will have to reference the MDN often until I get the hang of these things.