CodeFez presents
1.3.4 HTML, CSS and JavaScript
Lesson 2: JavaScript
Objectives
JavaScript (not Java)
Javascript is a scripting language that uses all of the same programming constructs that we are familiar with in other programming languages.
It is a simple language that can be used to add programming functions to a web page. This is especially useful with interactive elements on a page.
Add JS to HTML in 2 ways
inline
This is possible but it will confuse you more right now!
Body
Script is added in the body with <script> tags
External file Link
A JS file is linked to the HTML file in the header
Methods
JS Script in the Body
<html>�<head>
�</head>�<body>� <script language =”JavaScript”>
// code can go here
</script>
</body>�</html>
JS linked externally
<html>�<head>�
<script src=”some_JS_file.js”></script>��</head>�<body>�
</body>�</html>
Use of IDs in tags
In HTML tags we can assign IDs so that JS can refer to specific
parts of a webpage. �Look at the example here of how IDs are used for input and output to the page.
Example JavaScript Validation of Input
See on W3Schools
Example fairground height calc
This code states whether a person is too short to ride on a fairground rollercoaster if their height is below 5 feet.
It must convert the person’s height from centimetres first.
Task 1
Task 1
Take a look at the previous slide again and see if you can edit this to work out a person’s BMI based on appropriate inputs.
The app should output the BMI and the correct output. Eg.
A BMI of 25.0 or more is overweight, while the healthy range is 18.5 to 24.9. A BMI of lower than 18.5 is considered underweight.
EXT�How can you use CSS to style the app?
Task 2
In this task, I am giving you tasks that would be similar to the kind of task you may be expected to know in a exam situation. I have put together a template for you to work with. you will work on a page for a petshop. That asks for a pet's birth year. and returns how old the pet is based on the current year.
This JavaScript snippet of code gets current year:
var currentYear = new Date().getFullYear(); //gets the year value
template and instructions here: Pet Shop Task | AppFlowy
Some JS Fun
Have a go at this https://www.justpark.com/creative/reaction-time-test/
How do you think it’s made. (Just think about the logic behind it)
Break down the task
Task 3
help on the next slide
snippets of code to help
https://stackoverflow.com/a/41633001/3550161 working out the difference in time
https://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer - counting down to an event!
Another countdown timer - html - JavaScript timer for a quiz - Stack Overflow
Function to create a random number https://stackoverflow.com/questions/44314897/javascript-timer-for-a-quiz?noredirect=1&lq=1
https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp - Showing & hiding sections in HTML
This is better Hide and show DOM elements using a CSS class in JavaScript (attacomsian.com)