Javascript in the browser
Lesson Objectives
Thinking Programmatically
Pseudo code
Interactivity
Why Javascript
Execute Javascript in a web page
View logged messages in the browser
Send a message to the user in the browser
Get user input in the browser
Problem solving for a bigger program
Nested function invocations
Sequence of execution
Reset state
More on Functions
Basic Functions review
Loops and Functions
When/How to use a function in a program
Running a function from browser input
Thinking Programmatically
Pseudo Code
Pseudo code is the process of taking a larger solution and breaking it down into the programmable steps without actually writing any code.
Think about the larger solution as a whole, but as a series of steps that you would write out for a petulant child to follow
Write out the solution in plain English, breaking it down into as many tiny steps as possible. Remember, this child doesn't want to do the task. If there's any ambiguity, you're sunk
Create a flow chart
Try to separate each phrase/independent clause onto a different line
Identify the following:
Assertions
comments, probably
Conditionals
A question is asked. This tells us a conditional is coming
Looks for words like "if, unless, otherwise"
Think of all the possible outcomes of the situation
Each outcome represents an
if
,else if
, orelse
statement
Loops
Something is done multiple times
Look for words like "while, as long as, until"
Think of the child asking "am I done yet?"
Better to tell them "keep eating string beans until there are no more string beans" than to tell them to each individual string bean
Functions
We've oversimplified a step which could be broken out into multiple steps.
Identify data types
whenever you have a conditional, loop, or something you're keeping track of, identify its type
text (strings)
numbers (ints/floats)
true/false values (booleans)
collections of stuff (arrays)
(Optional) Try to convert each line into something that resembles code
Activity (10 min) + review
Pseudo Code the process for creating a peanut butter and jelly sandwich
Interactivity
Why Javascript
Javascript is the language used to make web pages interactive
Execute Javascript in a web page
Put your javascript in a
.js
file as normalReference the file inside a script tag inside the
<head>
tag of your html fileYou do:
Create a new folder called
interactivity
Add an
index.html
file, ajs
folder and ascript.js
file to this folder.Connect the
script.js
to yourindex.html
file and open theindex.html
file in your browser!
View logged messages in the browser
To see a message that you've created using console.log()
:
Open up the dev tools in your browser
Now any messages that you send using console.log()
will appear here (next to the carrot)
Send a message to the user in the browser
alert()
is a global function that will open up a message box in the browser
Notes: You can use this for testing and playing with javascript, but it's usually not a good idea to use alerts in practice because it locks the whole browser and stops the flow of code execution. They can also be turned off by the browser user, and most users don't read them thoroughly before clicking a button to close them anyway.
Get user input in the browser
prompt()
is like alert, but it opens up a window with a message AND a place to enter some text
You can use the return value of prompt
just like any other value
You can call a function based on some input from the browser prompt
You can keep getting input until a certain exists like so:
Problem solving for a bigger program
Nested function invocations
You can have functions call other functions:
Sequence of execution
Program execution happens in a branching structure. List what the following logs. See if you can diagram its execution:
Reset state
Sometimes you want to reset a situation to its original state
This can be done with a start function that gets called at the beginning and restarting of situation.
It should reset all values and begin process again
More on Functions
Basic Functions review
Functions encapsulate (isolate) a set of commands pertaining to one set of functionality
Get a users name
Add an item to your cart
Shoot the lizard creature
etc
Try not to have a function that "does" multiple things
Loops and Functions
When to use which?
A loop is used whenever you do something repeatedly
A function is used to simplify something more complex (e.g. Take the orange out of the fridge)
Functions in Loops
If executing a function multiple times, you don't need to define the function within the loop. Instead put it at the top
GOOD
BAD
Loops in Functions
You can have loops within a function:
When/How to use a function in a program
If you can try to have your functions defined at the top of the javascript file
At the bare minimum, make sure you create your functions before calling them
Good:
Meh:
Bad:
Last updated
Was this helpful?