JavaScript
JavaScript
HTML
• Hyper Text Markup Language
Text formatting
• the standard markup language for creating Web pages
• describes the structure of a Web page and consists of a series of elements
• HTML elements tell the browser how to display the content
<b> This text is bold </b> - defines bold text, without any extra importance
Basic structure
<strong> This text is important </strong> - defines text with strong importance
An HTML element is defined by a start tag, some content, and an end tag, for example:
<h1> My First Web page </h1> <i> This text is italic </i> - defines a part of text in an alternate voice
HTML elements with no content are called empty elements. <em> Emphasized text </em> - defines emphasized text
For example, the <br> tag defines a line break, and is an empty element without a closing tag.
<mark> Marked text </mark> - defines text that should be marked or highlighted
The HTML <ins> element defines a text that has been inserted into a document. Browsers will
JavaScript Page 1
HTML: Revision (lists, position and text alignment)
Monday, December 09, 2024 7:09 PM
1. Tea
2. Milk
3. Juice
Heading 1
the most important heading
Heading 6
the less important heading
JavaScript Page 2
HTML: Revision (Fonts – Face and Size and colors)
Monday, December 09, 2024 7:10 PM
The font tag is having three attributes called size, color, and face to customize your fonts.
Font Face
It is possible to set font face using face attribute but if the user viewing the page doesn't have
the font installed, they will not be able to see it.
For example (try in Editor):
<!DOCTYPE html>
<html>
<body>
<font face = "Times New Roman">Times New Roman</font><br>
<font face = "Arial">Arial</font><br>
<font face = "Comic Sans MS">Comic Sans MS</font>
</body>
</html>
Font Size
It is possible to set content font size using size attribute. The range of accepted values is from
1(smallest) to 7(largest). The default size of a font is 3.
For example (try in Editor):
<!DOCTYPE html>
<html>
<body>
<font size = "1">Font size = "1"</font><br>
<font size = "2">Font size = "2"</font><br>
<font size = "3">Font size = "3"</font><br>
<font size = "4">Font size = "4"</font><br>
<font size = "5">Font size = "5"</font><br>
<font size = "6">Font size = "6"</font><br>
<font size = "7">Font size = "7"</font>
</body>
</html>
Combination of the Font Face and Font Size
For example (try in Editor):
<!DOCTYPE html>
JavaScript Page 3
<!DOCTYPE html>
<html>
<body>
<font face = "Times New Roman" size = "7">Times New Roman</font>
<br>
<font face = "Calibri" size = "2">Calibri</font><br>
<font face = "Comic Sans MS" size =" 3">Comic Sans MS</font><br>
<font face = "Lucida Bright" size = "4">Lucida Bright</font><br>
<font face = "Arial" size = "7">Arial</font>
</body>
</html>
It is possible to set any font color you like using color attribute. You can specify the color
that you want by either the color name or hexadecimal code for that color.
There are two ways to define colors:
1. Using a color name from a set of 16 predefined names
2. Using the # symbol followed by a three-digit hexadecimal number where each of the three digits is a
two-digit hexadecimal number (eg # CC99FF,). Each of the three two-digit hexadecimal numbers
represents the shade of three colors: Red, Green and Blue and can take values from 00 to FF. This
color definition model is called RGB (Red, Green, Blue).
The first way we use it mainly when we want a main color and not a shade. There are 16
main colors that we can refer to by their name.
JavaScript Page 4
<html>
<body>
<font color = "#0099FF">This text is in blue! </font><br>
<font color = "#FF66FF">This text is in pink! </font><br>
<font color = "red">This text is red! </font><br>
<font color = "purple">This text is purple! </font>
</body>
</html>
JavaScript Page 5
#CCFF00 #CCFF33 #CCFF66 #CCFF99 #CCFFCC #CCFFFF
#FF0000 #FF0033 #FF0066 #FF0099 #FF00CC #FF00FF
#FF3300 #FF3333 #FF3366 #FF3399 #FF33CC #FF33FF
#FF6600 #FF6633 #FF6666 #FF6699 #FF66CC #FF66FF
#FF9900 #FF9933 #FF9966 #FF9999 #FF99CC #FF99FF
#FFCC00 #FFCC33 #FFCC66 #FFCC99 #FFCCCC #FFCCFF
#FFFF00 #FFFF33 #FFFF66 #FFFF99 #FFFFCC #FFFFFF
JavaScript Page 6
HTML: Revision (Images, links and comments)
Monday, December 09, 2024 7:10 PM
Images can improve the design and the appearance of a web page.
The HTML <img> tag is used to insert an image in a web page.
Example: <img src = "bird.jpg">
Attributes:
src Specifies the URL of the image file (path to the image)
alt Specifies an alternate text for the image
height Specifies the image height in pixels
width Specifies the image width in pixels
border Specifies border thickness in terms of pixels
With the background property of the body, we add the background image to our page
<body background = "bird.jpg">
JavaScript Page 7
For example: https://www.w3schools.com
<!DOCTYPE html>
<html>
<body>
<a href="https://www.w3schools.com/">Visit W3Schools.com!</a>
</body>
</html>
_self - opens the document in the same window/tab as it was clicked (default)
_blank - opens the document in a new window or tab
_parent - opens the document in the parent frame
_top - opens the document in the full body of the window
<!DOCTYPE html>
<html>
<head>
<title>Hyperlink Example</title>
</head>
<body>
<p>Click any of the following links</p>
<a href = "https://www.tutorialspoint.com/" target = "_blank">Opens in New</a>
<a href = "https://www.tutorialspoint.com/" target= "_self">Opens in Self</a>
<a href = "https://www.tutorialspoint.com/" target = "_parent">Opens in Parent</a>
<a href = "https://www.tutorialspoint.com/" target = "_top">Opens in Body</a>
</body>
</html>
Comments
HTML comments are not displayed in the browser.
They can help document your HTML source code.
The syntax: <!-- Comments -->
JavaScript Page 8
<body>
<p>This <!-- great text --> is a paragraph.</p>
</body>
</html>
JavaScript Page 9
HTML: Revision (Tables)
Monday, December 09, 2024 7:10 PM
HTML tables allow web developers to arrange data into rows and columns.
To set the width and height of a table, add the width and height (or style) attribute to the <table>
element.
To set the border attribute to the <table> element.
JavaScript Page 10
JavaScript Page 11
CSS: Revision (introduction)
Monday, December 09, 2024 7:11 PM
The key to understanding how CSS works is to imagine that there is an invisible box around every HTML
element. CSS Associates Style rules with HTML elements
CSS allows you to create rules that control the way that each individual box (and the contents of that
box) is presented. CSS works by associating rules with HTML elements. These rules govern how the content of specified
elements should be displayed. A CSS rule contains two parts: a selector and a declaration.
Selectors indicate which element the rule applies to. The same rule can apply to more than one element
if you separate the element names with commas.
Declarations indicate how the elements referred to in the selector should be styled. Declarations are
split into two parts (a property and a value), and are separated by a colon.
Properties indicate the aspects of the element you want to change. For example, colour, font, width,
height and border.
Values specify the settings you want to use for the chosen properties. For example, if you want to
specify a colour property then the value is the colour you want the text in these elements to be.
Using CSS, you could add a border around any of the boxes, specify its width and height, or add a
background colour. You could also control text inside a box — for example, its colour, size, and the
typeface used.
JavaScript Page 12
17/12: JavaScript - Introduction
Monday, December 09, 2024 7:12 PM
JavaScript
What is JavaScript? | JavaScript Tutorial for Beginners | JavaScript Training | Edureka
https://stackshare.io/
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
• Client-side validation,
• Dynamic drop-down menus,
• Displaying date and time,
• Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box and prompt
dialog box),
• Displaying clocks etc.
Source: https://www.javatpoint.com/javascript-tutorial
<script>
JavaScript code
</script>
Editor: https://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst
JavaScript Page 13
</script>
</body>
</html>
Example:
<script>
document.write("Hello JavaScript!");
</script>
EXAMPLE:
JavaScript Page 14
17/12: Comments and New line
Monday, December 09, 2024 7:12 PM
Comments
JavaScript Single line Comment
Single line comments start with //. Any text written between // and the end of line will be ignored by
JavaScript (will not be executed).
Example:
<script>
// It is single line comment
document.write("Hello JavaScript!");
</script>
Example:
/* your code here */
It can be used before, after and middle of the statement.
<script>
/* It is multi line comment.
It will not be displayed */
document.write("Hello JavaScript!");
</script>
New line
<script>
document.write("Hello");
document.write("<br>");
document.write("JavaScript!");
</script>
EXAMPLE 1.
1. Add a single line comment next to your age (you did in lesson before) saying: This is my age; and test
if it's okay
2. Add a multiline comment next to your name (you did in lesson before) saying:
This
Is
JavaScript Page 15
Is
My
Name
and test if it's okay
JavaScript Page 16
17/12: JavaScript Variables
Monday, December 09, 2024 7:12 PM
Variable naming
There are two limitations on variable names in JavaScript:
Variables are used to store data values. • The name must contain only letters, digits, or the symbols $ and _.
JavaScript uses the keywords var, let and const to declare variables. • The first character must not be a digit.
An equal sign is used to assign values to variables.
One of the most fundamental characteristics of a programming language is the set of data types it Examples of valid names:
supports. These are the type of values that can be represented and manipulated in a programming let userName;
language.
let test123;
Data types:
let $ = 1; // declared a variable with the name "$"
Number: Any number, including numbers with decimals: e.g., 123, 120.50 etc.
let _ = 2; // and now a variable with the name "_"
String: Any grouping of characters on your keyboard (letters, digits, spaces, symbols, etc.) surrounded by
single quotes: ' ... ' or double quotes " ... ", e.g. "This text string" etc.
Boolean: This data type only has two possible values— either true or false (without quotes). It’s helpful Examples of incorrect variable names:
to think of booleans as on and off switches or as the answers to a “yes” or “no” question. let 1a; // cannot start with a digit
Null: This data type represents the intentional absence of a value, and is represented by the keyword let my-name; // hyphens '-' aren't allowed in the name
null (without quotes).
Undefined: This data type is denoted by the keyword undefined (without quotes). It also represents the
absence of a value though it has a different use than null. Undefined means that a given value does not JavaScript variable names are case-sensitive: laura and LAuRa are two different variables.
exist. Also, when we have an variable made out more than one word, use Camel case (every next word of the
Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. name of the variable, its first letter is capital- e.g. getElementById
No need to worry about these for now.
Object: Collections of related data. Reserved names
There is a list of reserved keywords, which cannot be used as variable names because they are used by
Storing a value in a variable is called variable initialization. You can do variable initialization at the time the language itself. For example: let, class, return, and function are reserved.
of variable creation or at a later point in time when you need that variable.
Constants
Examples: To declare a constant (unchanging) variable, use const: const myBirthday = '8.08.1988';
1.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x is defined as a variable.
Then, x is assigned the value of 6:</p>
<script>
let x;
x = 6;
document.write (x)
</script>
</body>
</html>
OR
<script>
let x;
x = 6;
document.write (x)
</script>
2.
<script>
let message;
message = 'Hello!';
document.write (message);
</script>
<script>
let message = 'Hello!';
document.write (message);
</script>
<script>
let message;
message = 'Hello!';
document.write (message);
document.write("<br>");
message = 'World!'; // value changed
document.write (message);
</script>
JavaScript Page 17
17/12: JavaScript Operators
Monday, December 16, 2024 9:40 PM
JavaScript Page 18
14/1: Interaction: alert, prompt, confirm
Monday, December 16, 2024 9:41 PM
alert
Example:
<script>
alert( 'Hello, world!' );
</script>
prompt
The function prompt accepts two arguments:
result = prompt(title, [default]);
It shows a modal window with a text message, an input field for the visitor, and the buttons
OK/Cancel.
Title: The text to show the visitor.
Default: An optional second parameter, the initial value for the input field.
The square brackets in syntax [...] - The square brackets around default in the syntax above denote
that the parameter is optional, not required.
Example:
<script>
let age = prompt('How old are you?', 100);
</script>
The visitor can type something in the prompt input field and press OK. Then we get that text in the
result. Or they can cancel the input by pressing Cancel or hitting the Esc key, then we get null as the
result. The call to prompt returns the text from the input field or null if the input was canceled.
confirm
result = confirm(question);
The function confirm shows a modal window with a question and two buttons: OK and Cancel.
The result is true if OK is pressed and false otherwise.
Example:
<script>
JavaScript Page 19
let age = confirm("Are you 18?");
</script>
JavaScript Page 20
14/1: EXERCISES - examples
Monday, December 16, 2024 9:42 PM
Examples:
JavaScript Page 21
document.write ('d is ',d );
document.write("<br>");
document.write ('e is ',e );
document.write("<br>");
document.write (' f is ',f );
document.write("<br>");
document.write (' g is ',g );
document.write("<br>");
</script>
JavaScript Page 22
14/1: Sequence - exercises 1
Monday, December 16, 2024 9:42 PM
The Math.floor() static method always rounds down and returns the largest integer less
than or equal to a given number.
The Math.random() static method returns a number that's greater than or equal to 0 and less
than 1.
https://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst
1. Write the program that obtains from the user the date of today and displays the date in the
form dd/mm/yyyy.
<script>
let a= prompt( "Write the Day");
let b= prompt( "Write the Month") ;
let c= prompt( "Write the Year");
document.write(a,'/',b,'/',c) ;
</script>
2. Write the program that obtains from the user his year of birth and a year in the future, and
calculates how old will the user be in exact year in the future.
<script>
let birthYear = Number(prompt( "Write the Year of your Birth"));
let futureYear =Number(prompt( "Write the Year in the Future"));
let age = futureYear - birthYear;
document.write ('You will be ' + age + ' years old!' );
document.write ('<br>' );
document.write ('You will be ', age , ' years old!' );
document.write ('You will be ', futureYear - birthYear , ' years old!' );
</script>
3. Write the program that obtains from the user the temperature in Celsius degrees and
calculates and displays it in Fahrenheit degrees.
Fahrenheit = (Celsius*9)/5 + 32;
4. Write the program that obtains from the user the values of 3 numbers and calculates and
displays their sum, product, and average
<script>
let a=Number(prompt ("Write the 1st number: "));
let b= Number(prompt ("Write the 2nd number: "));
let c= Number(prompt ("Write the 3rd number: "));
let d=a+b+c;
let e= a*b*c
let f= d/3
document.write ("The sum is: ", d);
document.write("<br>");
document.write ("The product is: ", e);
document.write("<br>");
document.write ("The average is: ", f);
document.write("<br>");
</script>
OR
<script>
let a=Number(prompt ("Write the 1st number: "));
let b= Number(prompt ("Write the 2nd number: "));
let c= Number(prompt ("Write the 3rd number: "));
document.write ("The sum is: ", a+b+c);
document.write ("<br> The product is: ", a*b*c);
document.write ("<br> The average is: ", (a+b+c)/3);
</script>
JavaScript Page 23
28/1: CONDITIONAL STATEMENTS (DECISION)_1
Tuesday, January 28, 2025 10:11 AM
1. Write the program that displays a greeting Good morning! if the user enters a time before 10
(am).
2. Write the program that displays a greeting according to hour given by the user (Good
morning! if the user enters a time before 10 (am), else Good day!)
OR
3. Write a program that obtains from the user an integer and checks if the number is even or
odd. Then it displays the message 'EVEN NUMBER' or 'ODD NUMBER' accordingly.
JavaScript Page 24
28/1_4/2: CONDITIONAL STATEMENTS (DECISION)_2
Tuesday, January 28, 2025 10:13 AM
1. Write a program that obtains from the user his age and displays the message: Minor if the
age is under 18, Adult if the age is between 18 and 67 (18 and 67 included) and Elder
otherwise.
OR
2. Write a program that obtains from the user the values for 4 grades and calculates and
displays their average. It displays the messages accordingly:
< 10 Fail
10.1 - 14 D
14.1 – 15 C
15.1 – 17 B
17.1 – 20 A
JavaScript Page 25
4/2_11/2: CONDITIONAL STATEMENTS (DECISION)_3
Tuesday, January 28, 2025 10:15 AM
EXERCISES
1. Write a program that finds and displays the value of the largest of the three entered numbers.
2. Write a program that obtains from the user the values of three integer numbers and calculates
the sum of even numbers and sum of odd numbers.
3. Write a program that obtains from the user the values of three real numbers (positive numbers)
and checks if there is a triangle with these sides (length of the sides). If so, is it: scalene,
isosceles, or equilateral?
JavaScript Page 26
4. A company sells PCs at the price of € 500. For every order of 10 and more computers, it also
offers 5% discount as a gift. Write a program that obtains from the user the number of ordered
computers and calculates and displays the total amount he needs to pay.
5. Write a program that obtains from the user the values of two integer numbers a and b. If both
numbers are even divide the larger with the smaller number. If both are odd numbers, subtract
the smaller number from the larger one. Otherwise add those two numbers.
JavaScript Page 27
6. An employee receives a salary depending on the years of his service according to the following
table. Write a program that obtains from the user the employee's years of service and displays
the salary he will receive.
JavaScript Page 28
JavaScript Page 29
11/2_18/2: CONDITIONAL STATEMENTS (DECISION)_4
Monday, February 10, 2025 11:41 AM
EXERCISES
1. Write a program that obtains from the user a four-digit integer number and checks if the
number is a palindrome.
2. Write a program that obtains from the user a three-digit integer number. If the digit of the
units is an even number, the program should display the digit of hundreds, otherwise the
digit of tens.
JavaScript Page 30
4/3: Test: JavaScript_1
Tuesday, February 11, 2025 6:39 PM
JavaScript Page 31
18/2: LOOPS – While loops
Monday, February 10, 2025 12:59 PM
Loops - allow code to be executed repeatedly based on a given condition (true or false)
The While loop
The while loop has the following syntax:
while (condition)
{
// code
// so-called "loop body"
}
While the condition is true, the code in the loop body is executed.
Example:
Write a program that displays the numbers from 1 to 5.
If you forget to increase the variable used in the condition, the loop will repeat (in theory)
forever. In practice, the browser provides ways to stop such loops; the browser will crash.
Any expression or a variable can be a loop condition, not just comparisons: the condition is
evaluated and converted to a Boolean.
For example, a shorter way to write while (i != 0) is while (i):
JavaScript Page 32
18/2: EXERCISES – WHILE LOOP_1
Monday, February 10, 2025 3:08 PM
1. Write a program that displays only odd numbers from 1 to 18 (in the same row, separated by ,).
2. Write a program that obtains from the user numbers k and n and displays only numbers divisible by
3 between k to n (in the same row).
3. The ICT teacher is assigning the grades to the students in the first semester and wants to
calculate the average of the class and how many students have the grade A (grade over 18).
Grades range from 0 to 20. As the number of students is unknown, the teacher will stop
entering the grades when he writes the special value -1. Note: As the number of students is
not known in advance, it can be zero (0).
4. Write a program that obtains from the user one by one character as long as they are
different from the “dot (.)”, calculates and displays:
a) the number of characters
b) how many times the letter "A" or "a" was entered
JavaScript Page 33
LOOPS – Do...While loops
Monday, February 10, 2025 3:16 PM
The loop first executes the code in the body, then checks the condition, and, while the condition is true,
executes the code again and again.
The code will be executed at least once regardless of the condition being true or false.
Example:
Write a program that displays the numbers from 1 to 5.
3. Write a program that reads numbers from the keyboard, calculates, and displays their sum at
the end. Number entry stops when the sum exceeds 200.
JavaScript Page 34
Additional exercises
Monday, February 10, 2025 3:18 PM
1. Write a code for guessing game. User needs to guess the number in a range from 1 to
100, and at the end program should also display the number of attempts.
JavaScript Page 35