Week 2: Values, Types & Operators
To Do This Week:
Assignments:
Codecademy: Complete the lessons (not the projects) from the Learn JavaScript module:- "Welcome to Learn JavaScript"
- "Introduction to JavaScript", "Variables"
- "Conditionals"
Optional Reading:
Read chapters from Eloquent JavaScript:Super Survey
Javascript Demo: Lesson 1
Opening the Console:
- Windows: Ctrl-Shift-J
- Mac: Cmd-Option-J
- Check error with console
Animated Circles overview
- Circles Script - examine code step by step
- Circles Script - Letters
Values, Types and Operators
-
Built-ins / native functions:
alert,console.log,document.write,confirm -
Variables & input: declaring and reassigning with
letandconst; getting input withprompt. -
Strings: concatenation vs. template literals (backticks
`like this`). -
Core data types:
Number,Boolean,String,Arrays(quick console checks). -
Basic math:
+-*/and using a temporary variable. -
Shortcut operators:
++,--,+=,-=,*=,/=,%. -
Conditionals:
if/else,else if, and nesting. -
Comparison operators & equality pitfalls:
=,==,===,!=,!==,>=,<=. -
Modulus
%: odd/even checks (e.g., alternating turns).
W3Schools Tutorials
WORKSHOP
Within the JS Demo Lesson 1 are examples of short scripts with very basic uses of values, data types, operators, conditionals and simple arrays. Uncommenting scripts make them active to try. Make sure to comment again before moving to the next.
Using VS Code in this Class
Set Up Your HTML File
- Open a new HTML document and save it in your DTC 477 folder.
- If VS Code shows Restricted Mode, select it and choose to trust the folder.
- Type ! and press Tab or Enter to generate the basic HTML boilerplate.
-
Save the file with the .html extension, for example:
yourfilename.html. - Use the Live Server extension to open the HTML file in your browser.
- Use the browser's Console to check JavaScript errors.
Useful VS Code Shortcuts
- Line comment //: Ctrl + / (Windows) / Command + / (Mac)
- Block comment /* */: Shift + Alt + A (Windows) / Shift + Option + A (Mac)
- Full Screen (toggle): F11 (Windows) / Control + Command + F (Mac)
- Zen Mode: Ctrl + K, then Z (Windows) / Command + K, then Z (Mac)
- Exit Zen Mode: Esc twice
Annoying Code Suggestions
- Press Tab to accept a useful suggestion.
- Press Esc to dismiss a suggestion and keep typing.
- Show code suggestions: Ctrl + Space
- To disable ghost-text suggestions, open Settings: Ctrl + , (Windows) or Command + , (Mac). Search for Inline Suggest and turn off Editor: Inline Suggest Enabled.
In-Class Challenges
Below are JS mini-challenges. In a new HTML document, use variables, data types (numbers, strings and booleans), operators, conditionals and arrays to complete them.
Challenge 1: Personal Greeting
- Ask the viewer for their name using
prompt(). - Display a greeting on the page that includes their name using
document.write().
Challenge 2: Age Calculator
- Ask the viewer for their age using
prompt(). - Convert the string input to a number using
parseInt(), then calculate how old they will be in 10 years. -
Display a message on the page using
document.write()that includes:- Their current age
- Their age in 10 years
Note: parseInt() turns a string containing a number into an integer that JavaScript can use for math.
let number = "8";
let integer = parseInt(number);
Challenge 3: Yes / No Decision
- Ask the viewer a yes/no question using
confirm(). - Use an
if / elsestatement to display a different message on the page depending on their answer.
Challenge 4: Favorite Movies List — Arrays
- Create an array containing three of your favorite movies as strings.
-
Display:
- How many items are in the array
- The first item in the array
- The last item in the array
Hint: To access the last item, use array.length - 1.
Rock Paper Scissors Game Steps
- Ask the user to enter either "rock", "paper", or "scissors". Saved as prompt variable
- Create a list (array) of possible choices: "rock", "paper", "scissors".
- Have the computer randomly pick one option from that list.
- Save the computer’s choice in another variable.
- Print (or log) both the user’s choice and the computer’s choice so they are visible.
-
Compare the two choices:
- If both choices are the same, declare a Tie.
- If the user chooses "rock" and the computer chooses "scissors", the user wins.
- If the user chooses "paper" and the computer chooses "rock", the user wins.
- If the user chooses "scissors" and the computer chooses "paper", the user wins.
- In all other cases, the computer wins.
- Print the outcome: "User wins", "Computer wins", or "Tie".