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:Module Notes
Animated Circles overview
- Circles Script - examine code step by step
- Circles Script - Letters
Opening the Console:
- Windows: Ctrl-Shift-J
- Mac: Cmd-Option-J
- Open Circles Script and make an error
- Check error with console
Javascript Demo: Lesson 1
With the demo file today are examples of short scripts that demonstrate basic uses of values, data types, operators and conditionals. You follow by also uncommenting the commented out script. Make sure comment again before moving to the next.
After this you will open up a new HTML page to try out your own use of these scripts. In Sublime Text, save a new HTML file and then type "html" & press the tab key to generate boilerplate. Save in a "DTC 477" folder for future reference.
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
- Sandbox Tasks : use variables, data types (numbers, strings and booleans), operators and conditionals.
- JS Demo Lesson 1
- Develop idea for Interactive Website Project: describe logic steps
Challenge 1: Personal Greeting
Ask the viewer for their name using a prompt.
Store the name in a variable and display a greeting that includes their name.
Example output:
Hello Alex, welcome to the page!
Challenge 2: Age Calculator
Ask the viewer for their age.
Convert the string input to a number (parsInt("10")), then calculate how old they will be in 10 years.
Display both their current age and their age in 10 years.
Example output:
You are 19 years old. In 10 years, you will be 29.
Challenge 3: Yes / No Decision
Ask the viewer a yes/no question using a confirm box.
Use an if / else statement to display a different message depending on their answer.
Example:
Do you like movies?
Challenge 4: Name Inspector
Ask the user for their name using a prompt. Display:
- The length of the name
- The first letter of the name
- The name in ALL CAPS
- The name in lowercase
Challenge 5: Favorite Foods List
Create an array with three of your favorite foods (strings). Display:
- How many items are in the array
- The first item in the array
- The last item in the array
Hint: the last item uses array.length - 1.
Rock Paper Scissors Game Steps
- Ask the user to enter either "rock", "paper", or "scissors".
- Save the user’s choice in a 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".
Project 1: Interactive Website (5%)
This assignment brings together syntax, variables, conditionals, arrays, functions, and loops in JavaScript. Create a dynamic website with user interaction that changes content on the page.
Suggested Ideas:
- Interactive Quiz
- Dynamic Media Gallery
- To-Do List
- Customizable Timer
- Generative or Digital Art Work
- Branching Narrative
Working with ChatGPT:
- Describe your idea and layout for HTML structure.
- Detail your JavaScript steps in plain language.
- Start coding with ChatGPT’s guidance.
- Iteratively refine and troubleshoot.