YEAR 11 DIGITAL TECHNOLOGY
  • Computer Literacy
    • Basics
    • File Types
    • Compression
  • Animation
    • Adobe Illustrator >
      • Intro to Illustrator
      • Review of Illustrator Techniques
      • Illustrator - The Third
      • Advanced Illustrator
    • Adobe After Effects >
      • Basic Animation
      • Export to Youtube
      • Rigging
      • Animation Project
    • Project Documentation >
      • Purpose, Potential Users, Requirement and Specifications
      • Testing and Refinement
    • AS92005 - Develop a Digital technologies outcome
  • HTML/CSS Basics
    • HTML
    • CSS
    • Classes
  • Programming
    • Learn Basics - Variables and Prompts
    • If Statements
    • For Loops & Basic Arrays
    • While Loops & Functions
    • Robustness
    • Debugging
    • Skills Test
    • Testing and Documentation
  • HCI
    • AS92006 - HCI Exam
    • Introduction-Describe-Identify
    • Neilson's Heuristics
    • mātāpono Māori
    • Explaining Principles
    • Compare/Improvements
    • Practice Practice Practice
  • Data
    • Learn Excel
    • Learn Excel (Part Deux)
    • Practice Assessment
    • Practice Assessment 2 - police
    • Assessment
  • Freyberg Digital

Code commenting

As we talked about in a previous lesson, it is very important to name our variables in a way that lets other people reading our code know what they are for. However, sometimes we will write code that needs some further explanation, to do this we use comments. In JS we can write comments using two backslashes like below.
Picture
Task: Update your GST calculator to have comments explaining what is happening

"If" Statements

Remember on the last page when we talked about Boolean values? These are True/False values and are a very important part of if statements.

When we use if statements in JS we are testing whether a given statement is true or false, then when we have determined which it is we will run a set piece of code. In JS an if statement follows the following structure:

if (condition) {
    code to run
}


All your life you have already been using if statements, for example:

If you are smelly then take a shower.
If the cake is golden brown, then take it out of the oven.
If your health is 0 then game over.

We can show this using a flow diagram like the one on the right. We use diamonds (or a square on its side) to show a decision point. Here we are checking whether someone is old enough to play GTA V.
Picture
Task: Create code to ask the user for their age to determine whether they are old enough to get their drivers licence or not.

Else

In code we can use else after an if statement to account for what happens in case our "if" test is false. Using a real life example: if it's raining at lunch then you will likely eat under cover, else you might eat in the sun.
Picture
Task:
1) Using the same format as your previous html file, create a new file called colours.html file.
2) Write code to ask a user for their favourite colour, if they enter green it should say something nice, if they enter anything else it should tell them that their choice is bad.


Show your teacher once you have done this.

Brief: Justice League

Task: Write a program that asks the user to enter the name of their favorite superhero and stores this in a variable.
If the entered superhero is "Batman", display a message saying "Welcome to the Justice League Batman!" Otherwise, display a message saying "Sorry, you're not part of our alliance yet."
Picture

Creating a button

In this section we are going to learn how to use a button on our page to do something. Create an HTML file with an h1 tag saying "Business App" and the following HTML tag:
        <button type="button"> Click Me </button>
Load the page and have a look, it should look like the image below.
Picture
Add an onClick property to the button like below.
<button type="button" onClick="sayHi()" > Click Me </button>

Adding this means that the code sayHi() will run when the button is clicked.

Here is how we make the code for sayHi():
function sayHi() {
    
}


Whatever is between the { } brackets will run when the button is clicked.
Picture
Task: add another button and another function to your business app which when clicked shows the user "Bye".
Picture

Activity: Please enter the password.

Task: Make a password protected program using if and else.

1.) Make a button that calls a function called getPassword() like you did for sayHi() in the last activity.
2.) Use a prompt to ask the user for the password and store it in a variable (call the variable password.)
3.) In your getPassword() function enter this code:
   if (password == `weetbix`) {
        
    } else {
        
    }
   

4.) If the user enters the correct password then alert them with a secret message.
5.) If they have entered the incorrect password give them another message telling them that they are wrong.

Picture
Task: Age Verification Checker.
 
Create an app that asks the user their age.

If their age is below 18, tell them they are not allowed to play GTA V

You will need to use:
- Number()
- Greater than ">" and less than or equal to "<=" instead of equals "=="
See the table in the next section below for more information on these.

Else If

Sometimes there might be multiple things that we need to check, we can do this with an else if.
Picture
Task: Another Password

1.) Open up the previous activity on passwords.

2.) Add an else if to your weetbix program so that if they enter the fruitloops they get a super secret message.

Picture
Task: Using the flow chart and the relational operators table above, use else if statements to write some code that checks whether or not you are old enough to play GTA V. Get your teacher to check it!

New CODE:
isNaN() will tell you if something is a number or not.
Check this w3 link for more information.


Brief: Expanding the Justice League

Task: Extend your previous Justice League program to include two "else if" parts.
If the entered superhero is "Wonder Woman", display "Welcome, Amazonian Warrior!"
If the superhero is "Superman", display "Welcome to the Justice League Superman!"
​For all other inputs, display "Sorry, you're not part of the alliance yet."
Picture

isNan - is not a number

In programming it's often important to check that a variable is what we want it to be, before we do something with it. For example, if we're doing maths, we probably want to check that the user is actually entering numbers. isNan() lets us find out!
Picture
Task: create 2 variables with prompts asking the user for a number. Check that both of them actually have numbers, then multiply them together and put the answer in an alert.

Hint: you might need use Number() to turn what was entered into an actual number like the example on the right.

​
Picture

IsNaN practice

Go through the below variables and use IsNaN() and alert() to see whether it returns true (it's not a number) or false (it is a number)
let num1 = "12345";
let num2 = "12q34";
let num3 = 4567;
let num4 = true;
let num5 = "ONE";

Code Sandwiches & Bug Fixing

So by now you would have noticed the symbols { and } come up a lot.

These symbols are essential and you need to make sure you get them in the right place.

For Example compare these two pieces of code:
 if (password == "help") {
  alert("The secret is cheese");
} 
if (password == "help") {}
  alert("The secret is cheese");
Question: What is the difference between each piece of code? What is each one going to do?

Also consider these two pieces of code:
 if (password == "help")
  alert("The secret is cheese");
}
 if (password == "help") {
  alert("The secret is cheese");
Neither of these would work, in fact the whole program would not do anything. Why? What is wrong with the code?

Code Sandwiches

Using the concept of a code sandwich

You must have a top bun that looks like this:

if (guess != 22) {

And a bottom bun that looks like this:
}

The content all goes in the middle

Keep in mind you can have a sandwich inside a sandwich!
Picture
Picture
Activity: Download file and solve the problems with all the programs
activities_bug_finding_if_statements.zip
File Size: 3 kb
File Type: zip
Download File


Lastly ANDs & ORs

When using if statements we can use ||, &&, and ! to compare more variables together.

&& - means "AND" 

|| - means "OR" 

You won't burn your feet if you're wearing jandals OR gumboots. You're strange if you wear jandals AND gumboots.

Consider the code below:
Picture
Picture
Note: "!= True" is the same as "== False"
This means that we can write code like this:
if (raining == False) {
    alert("It is not raining");
}
if (raining != True) {
    alert("It is not raining");
}
Picture
Task: Create a program that gives correct feedback to this question.

"Name one colour in the New Zealand flag!!!"

Task: using a single if statement, make a program that gives the correct answer to the below questions:

"Name a colour in the New Zealand flag that begins with the letter r."
"Name a colour in the New Zealand flag that begins with the letter w."
"Name a colour in the New Zealand flag that begins with the letter b."

Hint: Use the code layout on the left but add in another check.

Brief: Check age for space travel

Write a program that asks the user to enter their age.
Use
isNaN to check if the age entered is a valid number (rather than text). If the input is not a number, display "Please enter a valid number for age."
​If the input is a valid number and is over 20, display "Age verified. Welcome aboard!"

Brief: Access Agent 47s Lair

Brief: The Secret Lair has strict access requirements. Write a program (using && and ||) that lets the user into different levels of the lair, depending on their clearance. Use the below code to get you started.

let accessLevel = Number(prompt("Please enter your level of access (1, 2, or 3)"));
let assassinQuestion = prompt("Are you an assassin? (This is an honesty based system, please be honest)");

Tasks:
1) If the user has access level 3 and is an assassin, display "Access granted."
2) If the user has access level 2 or is an assassin, display "Limited access granted."
3) For all other combinations, display "Access denied." Show your program to the teacher once completed.

Final task: Verification Final Security Check

You must make a program that does the following:
​
1.) Asks for the user name
2.) Asks for a password
3.) Converts the user name to lower case
4.) If the user is steve it kicks them out
6.) If Sam is the user with the password "NooDles" it gives him a welcome message
7.) If Sarah is the user with the password "dankMemes" it gives her a welcome message
8.) If they enter a number at any point or if they don't enter anything (blank) it should tell them off.

Show your work to your teacher
Picture
Powered by Create your own unique website with customizable templates.
  • Computer Literacy
    • Basics
    • File Types
    • Compression
  • Animation
    • Adobe Illustrator >
      • Intro to Illustrator
      • Review of Illustrator Techniques
      • Illustrator - The Third
      • Advanced Illustrator
    • Adobe After Effects >
      • Basic Animation
      • Export to Youtube
      • Rigging
      • Animation Project
    • Project Documentation >
      • Purpose, Potential Users, Requirement and Specifications
      • Testing and Refinement
    • AS92005 - Develop a Digital technologies outcome
  • HTML/CSS Basics
    • HTML
    • CSS
    • Classes
  • Programming
    • Learn Basics - Variables and Prompts
    • If Statements
    • For Loops & Basic Arrays
    • While Loops & Functions
    • Robustness
    • Debugging
    • Skills Test
    • Testing and Documentation
  • HCI
    • AS92006 - HCI Exam
    • Introduction-Describe-Identify
    • Neilson's Heuristics
    • mātāpono Māori
    • Explaining Principles
    • Compare/Improvements
    • Practice Practice Practice
  • Data
    • Learn Excel
    • Learn Excel (Part Deux)
    • Practice Assessment
    • Practice Assessment 2 - police
    • Assessment
  • Freyberg Digital