JavaScript For Humans by Christopher Topalian
JavaScript For Humans by Christopher Topalian
FOR
HUMANS
by
Christopher Topalian
All Rights Reserved
Copyright 2000 - 2021
Dedicated
to
God the Father
Table of Contents
Make a New Folder...............................................................7
Text Editor...........................................................................8
ourFirst.html page..............................................................10
Our First JavaScript Code....................................................11
function in head.................................................................12
function in body.................................................................13
Our Function defined in the HEAD.......................................14
Our Function defined in the BODY.......................................15
Our Function is Like a Sandwich..........................................16
return, simple.....................................................................17
return, simple, with parameter............................................18
return, simple, with parameter............................................19
return, simple, with parameter............................................20
function in head and return.................................................21
What is the onload event?...................................................22
onclick event for our Button................................................23
Button with Counter............................................................24
ask and say name...............................................................26
prompt if simple.................................................................27
ask and say name, if name..................................................28
enter a number, return.......................................................29
return, absolute number.....................................................31
User Input using textarea....................................................32
How to Design Applications.................................................34
date get Day......................................................................35
save as dateGetDayName.js................................................38
get Day, uses dateGetDayName.js file..................................40
get Day, using a parameter.................................................41
save as dateGetMonthName.js............................................42
get Month, using 2 parameters............................................44
get Day, get Month, using 2 parameters..............................45
Position Divs Using Style for the Month and Day...................46
month and day using onclick event......................................48
get month, get day, styled nice...........................................50
get month, get day, styled more..........................................52
save as ourStyle.css............................................................54
saved as showInfo.js..........................................................58
get month, get day, using externals.....................................59
saved as timeGetLocalTime.js..............................................61
get Day, get Month, get Time, onclick..................................63
get Day, get Month, get Time, continually............................65
saved as dateDayMessage.js...............................................67
uses dateGetName.js, dateDayMessage.js, showInfo.js.........69
save as dateMonthMessage.js.............................................70
uses many external js files!.................................................73
Planning Bigger Projects......................................................75
save as ourSimpleFunction.js...............................................76
function ourSimpleFunction()...............................................76
uses ourSimpleFunction.js...................................................77
save as index.html..............................................................78
File Structure of ourProject..................................................79
File Structure of our js files.................................................80
File Structure of our html files.............................................81
The File Structure Advantage..............................................82
save as mathNumberSquare.js............................................83
uses mathNumberSquare.js.................................................84
The class method is useful, but...........................................85
save as mathNumberSquare1.js...........................................86
uses as mathNumberSquare1.js...........................................87
Combine all js files into one.................................................88
This new file contains all of our .js files in ONE file. This can
make deploying our website much easier!............................88
Foundational JavaScript Tutorials.........................................89
confirm with location.href....................................................90
ask and say name...............................................................91
date and time, simple.........................................................92
do while prompt, enter name..............................................93
do while prompt, enter name..............................................94
do while using and..............................................................96
do while using Not Or And...................................................97
do while location.href..........................................................99
drop down menu..............................................................100
Either or nickname............................................................102
Find this word, Cat............................................................104
if else choose...................................................................105
if else choose...................................................................106
navigator for info..............................................................107
Length of a String.............................................................108
window, new window........................................................109
window, new window, same as this...................................110
username && password....................................................111
password using not equal to..............................................112
quiz question math...........................................................113
checkboxes......................................................................114
array, sort........................................................................116
if many............................................................................117
textbox value....................................................................119
Enter Name, toLowerCase()...............................................120
combining strings..............................................................121
combining strings prompt..................................................122
while count down..............................................................123
while count up..................................................................124
quick math question..........................................................125
which teams won?............................................................126
show all records as JSON..................................................128
show Records for loop and filter........................................130
show Records for loop and filter........................................132
show Records from and too...............................................134
show record.....................................................................136
hour and minutes.............................................................138
date month message.........................................................140
hour message...................................................................143
class simple......................................................................145
class................................................................................146
class inheritance, extends..................................................148
class inheritance, extends person.......................................152
convert, f to c...................................................................155
class simple, and this........................................................157
keyboard control...............................................................158
keyboard, move text.........................................................160
saved as timeGetTime.js...................................................163
Keep Getting Time uses timeGetTime.js.............................176
save as timeMessage.js.....................................................177
time of day message, uses timeMessage.js.........................179
Make a New Folder
VS CODE
https://code.visualstudio.com/
ATOM
https://atom.io/
BRACKETS
http://brackets.io/
Sublime
https://www.sublimetext.com/
Notepad++
https://notepad-plus-plus.org/
downloads/
<html>
<head>
<title>Our First WebPage</title>
</head>
<body>
</body>
</html>
<html>
<head>
<title>Message Box</title>
</head>
<body>
<script>
alert("Hi everyone");
</script>
</body>
Hi everyone
</html>
OK
<body>
<script>
showText();
</script>
</body>
</html>
function in body
<html>
<head>
<title> write function in body </title>
</head>
<body>
<script>
document.write("Hi Everyone");
</script>
</body>
</html>
Our Function defined in the HEAD
<html>
<head>
<title>Message Box</title>
<script>
function ourMessage()
{
alert("Hi everyone");
}
</script> Hi everyone
</head> OK
</body>
</html>
Our Function defined in the BODY
<html>
<head>
<title>Message Box</title>
</head>
<script>
function ourMessage()
{
alert("Hi everyone");
}
</script>
Hi everyone
</body>
OK
</html>
Our Function is Like a Sandwich
function ourMessage()
{
alert("Hi everyone");
}
<html><body>
<script>
function simple()
{
return "howdy";
}
alert(simple());
</script>
</body>
</html>
return, simple, with parameter
<html>
<body>
<script>
function simple(yourMessage)
{
return yourMessage;
}
alert(simple("Hi Everyone"));
</script>
</body>
</html>
return, simple, with parameter
<html>
<body>
<script>
function simple(yourMessage)
{
let greeting = "Hi there " +
yourMessage;
return yourMessage;
}
</script>
</body>
</html>
return, simple, with parameter
<html>
<body>
<script>
function simple()
{
let person = prompt("Enter your
Name?", "");
let greeting = "Howdy " + person;
return greeting;
}
alert(simple());
</script>
</body>
</html>
function in head and return
function ourMessage(){
return "Hi Everyone";
}
function showText(){
document.write(ourMessage());
}
</script>
</head>
<body onload="showText();">
</body></html>
What is the onload event?
<html>
<head>
<title>Button Press</title>
<script>
function ourFunction()
{
alert("Hi Everyone");
}
</script>
</head>
<body>
</body></html>
Button with Counter
<html><head><title>Button Press</title>
<script>
function ourFunction()
{
ourCounter += 1;
document.getElementById("displayText").
value = theText + ourCounter;
}
</script></head>
<body>
</body></html>
ask and say name
<html>
<head>
<title> Ask and Say Name </title>
</head>
<body>
<script>
</script>
</body></html>
prompt if simple
<html><head><title> Prompt
function</title>
</head>
<body>
<script>
let name = prompt("Enter your name");
if (name == "chris")
{
document.write("Hi Chris");
}
</script>
</body>
</html>
ask and say name, if name
<html><head>
<title>Ask, Say name, if person,
else</title>
</head>
<body>
<script>
let name = prompt("Enter your name
","");
if (name == "chris") {
alert("Hi " + name);
}
else {
alert("Where is Chris?");
}
</script>
</body>
</html>
enter a number, return
<html><head>
<script>
function askNumber()
{
let question = prompt("Enter a number",
"");
return question;
}
</script>
</head>
<body>
<button onclick="displayIt('div1',
askNumber());">
Enter Number
</button>
<div id="div1"></div>
</body></html>
return, absolute number
<html><body><script>
function ourFunction()
{
let question = prompt("Enter a number",
"");
let answer = Math.abs(question);
return answer;
}
<script>
function ourFunction()
{
let theText =
document.getElementById("enteredText").v
alue;
document.getElementById("displayText").
value = theText;
}
</script></head>
<body>
</body></html>
How to Design Applications
Do we start in code? No.
<html><head><script>
function dateGetDayName()
{
let theDate = new Date();
if(theDay == 0) {
return "Sunday";
}
if(theDay == 1) {
return "Monday";
}
if(theDay == 2) {
return "Tuesday";
}
if(theDay == 3) {
return "Wednesday";
}
if(theDay == 4) {
return "Thursday";
}
if(theDay == 5) {
return "Friday";
}
if(theDay == 6) {
return "Saturday";
}
}
function showInfo()
{
document.getElementById("day").innerHT
ML = dateGetDayName();
}
</script>
</head>
<div id = "day">Day</div>
</body></html>
<!--
-->
save as dateGetDayName.js
function dateGetDayName()
{
let theDate = new Date();
let theDay = theDate.getDay();
if(theDay == 0) {
return "Sunday";
}
if(theDay == 1) {
return "Monday";
}
if(theDay == 2) {
return "Tuesday";
}
if(theDay == 3) {
return "Wednesday";
}
if(theDay == 4) {
return "Thursday";
}
if(theDay == 5) {
return "Friday";
}
if(theDay == 6) {
return "Saturday";
}
}
<script>
function showInfo()
{
document.getElementById("dayDiv").inner
HTML = "It is " + dateGetDayName();
}
</script>
</head>
<div id = "dayDiv">Day</div>
</body></html>
get Day, using a parameter
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script>
function showInfo(whichDiv)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + dateGetDayName();
}
</script></head>
<div id = "dayDiv">Day</div>
</body>
</html>
<!-- We use a parameter in our function,
instead of defining it in our function.
Instead, it is defined when we use the
function. -->
save as dateGetMonthName.js
function dateGetMonthName()
{
let theDate = new Date();
let theMonth = theDate.getMonth();
if(theMonth == 0) {
return "January";
}
if(theMonth == 1) {
return "February";
}
if(theMonth == 2) {
return "March";
}
if(theMonth == 3) {
return "April";
}
if(theMonth == 4) {
return "May";
}
if(theMonth == 5) {
return "June";
}
if(theMonth == 6) {
return "July";
}
if(theMonth == 7) {
return "August";
}
if(theMonth == 8) {
return "September";
}
if(theMonth == 9) {
return "October";
}
if(theMonth == 10) {
return "November";
}
if(theMonth == 11) {
return "December";
}
}
get Month, using 2 parameters
<html><head>
<script src =
"../js/dateGetMonthName.js"></script>
<script>
function showInfo(whichDiv,
functionName)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script>
</head>
<div id = "monthDiv">Month</div>
</body></html>
get Day, get Month, using 2
parameters
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<script>
function showInfo(whichDiv,
functionName)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script></head>
<body onload = "showInfo('monthDiv',
dateGetMonthName()); showInfo('dayDiv',
dateGetDayName())">
<div id = "monthDiv">Month</div>
<div id = "dayDiv">Day</div>
</body></html>
Position Divs Using Style for the
Month and Day
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<script>
function showInfo(whichDiv,
functionName)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script>
<style>
.monthStyle
{
position:absolute;
top: 100px;
left: 100px;
}
.dayStyle
{
position:absolute;
top: 200px;
left: 100px;
}
</style>
</head>
<div id = "monthDiv"
class="monthStyle">Month</div>
<div id = "dayDiv"
class="dayStyle">Day</div>
</body></html>
month and day using onclick
event
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<script>
function showInfo(whichDiv,
functionName)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script>
<style>
.monthStyle
{
position:absolute;
top: 100px;
left: 100px;
}
.dayStyle
{
position:absolute;
top: 150px;
left: 100px;
}
</style></head>
<body>
</body></html>
<script>
function showInfo(whichDiv,
functionName)
{
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script>
<style>
.monthStyle {
position:absolute;
top: 100px;
left: 100px;
width: 80px;
border: 5px solid rgb(150,0,150);
border-radius: 5px;
}
.dayStyle {
position:absolute;
top: 150px;
left: 100px;
width: 80px;
border: 5px solid rgb(0,0,0);
border-radius: 5px;
}
</style></head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName())">Month</div>
<script>
function showInfo(whichDiv,
functionName) {
document.getElementById(whichDiv).inne
rHTML = "It is " + functionName;
}
</script>
<style>
div{
border: 5px solid rgb(0,0,0);
border-radius: 5px;
text-align: center;
}
div:hover {
border: 5px solid rgb(17, 0, 255);
}
.monthStyle {
position:absolute;
top: 100px;
left: 100px;
width: 80px;
}
.dayStyle {
position:absolute;
top: 150px;
left: 100px;
width: 80px;
}
</style></head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName())">Month</div>
<div id = "dayDiv" class="dayStyle"
onclick = "showInfo('dayDiv',
dateGetDayName())">Day</div>
</body></html>
save as ourStyle.css
div{
border: 5px solid rgb(0,0,0);
border-radius: 5px;
text-align: center;
}
body{
background-color: rgb(196, 196, 196);
}
div:hover {
border: 5px solid rgb(17, 0, 255);
}
.monthStyle {
position:absolute;
top: 100px;
left: 100px;
width: 80px;
}
.dayStyle {
position:absolute;
top: 150px;
left: 100px;
width: 80px;
}
/*
We save this file as ourStyle.css in
our css folder.
*/
/*
We make a folder named css in
ourProject folder. We save
ourSte.css inside of the css folder.
get month, get day, css external
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<link rel="stylesheet"
href="../css/ourStyle.css">
<script>
function showInfo(whichDiv,
functionName, ourText)
{
document.getElementById(whichDiv).inne
rHTML = ourText + ' ' + functionName;
}
</script>
</head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName(), 'It is
')">Month</div>
</body></html>
function showInfo(whichDiv,
functionName, ourText)
{
document.getElementById(whichDiv).inne
rHTML = ourText + ' ' + functionName;
}
<script src =
"../js/dateGetMonthName.js"></script>
<link rel="stylesheet"
href="../css/ourStyle.css">
</head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName(), 'It is
')">Month</div>
theDate.toLocaleString();
return theDate;
}
function timeGetLocalTimeRepeat()
{
let theDate = new Date();
theDate.toLocaleString();
document.getElementById("timeDiv").inne
rHTML = theDate;
//console.log(theDate);
}
function activateTimer()
{
let ourTimer =
setInterval(timeGetLocalTimeRepeat,
1000);
}
get Day, get Month, get Time,
onclick
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<script src =
"../js/timeGetLocalTime.js"></script>
<script src = "showInfo.js"></script>
<link rel="stylesheet"
href="../css/ourStyle.css">
</head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName(), 'It is
')">Month</div>
<div id = "dayDiv" class="dayStyle"
onclick = "showInfo('dayDiv',
dateGetDayName(), 'Today is
')">Day</div>
</body></html>
get Day, get Month, get Time,
continually
<html><head>
<script src =
"../js/dateGetDayName.js"></script>
<script src =
"../js/dateGetMonthName.js"></script>
<script src =
"../js/timeGetLocalTime.js"></script>
<script src = "showInfo.js"></script>
<link rel="stylesheet"
href="../css/ourStyle.css">
</head>
<body>
<div id = "monthDiv" class="monthStyle"
onclick = "showInfo('monthDiv',
dateGetMonthName(), 'It is
');">Month</div>
<div id = "dayDiv" class="dayStyle"
onclick = "showInfo('dayDiv',
dateGetDayName(), 'Today is
');">Day</div>
</body></html>
saved as dateDayMessage.js
function messageFromDay()
{
if(dateGetDayName() == "Friday")
{
return "It's Friday. Good time to learn
something new.";
}
else if(dateGetDayName() ==
"Saturday")
{
return "It's Saturday. Good time for
Scripting.";
}
else if(dateGetDayName() == "Sunday")
{
return "It's Sunday. Happy
Scripting.";
}
else if(dateGetDayName() == "Monday")
{
return "It's Monday. Time to learn.";
}
else if(dateGetDayName() == "Tuesday")
{
return "It's Tuesday. Learning alot of
Javascript.";
}
else if(dateGetDayName() ==
"Wednesday")
{
return "It's Wednesday. JSON is fun
to learn.";
}
else if(dateGetDayName() ==
"Thursday")
{
return "It's Thursday. One more day
till Friday.";
}
}
uses dateGetName.js,
dateDayMessage.js, showInfo.js
<html>
<body style="background-color:rgb(140,
192, 192);">
<script
src="../js/dateGetDayName.js"></script>
<script
src="../js/dateDayMessage.js"></script>
<script src="../js/showInfo.js"></script>
<button
onclick="showInfo('showDayMessage',
messageFromDay(), 'hi')">Press</button>
function messageFromMonth()
{
if(dateGetMonth() == "March")
{
return "It's March. Spring has come.";
}
else if(dateGetMonth() == "April")
{
return "It's April. Getting Warmer
out.";
}
else if(dateGetMonth() == "May")
{
return "It's May. Very nice out there
now.";
}
else if(dateGetMonth() == "June")
{
return "It's June. Summer is here.";
}
else if(dateGetMonth() == "July")
{
return "It's July. A Nice Warm
Summer so far.";
}
else if(dateGetMonth() == "August")
{
return "It's August. Summer is almost
over.";
}
else if(dateGetMonth() == "September")
{
return "It's September. Autumn has
come.";
}
else if(dateGetMonth() == "October")
{
return "It's October. It's getting cooler
out there.";
}
else if(dateGetMonth() == "November")
{
return "It's November. Winter mis
almost here.";
}
else if(dateGetMonth() == "December")
{
return "It's December. Winter is
here.";
}
else if(dateGetMonth() == "January")
{
return "It's January. It's not a snowy
winter.";
}
else if(dateGetMonth() == "February")
{
return "It's February. There is not
much snow out there.";
}
}
uses many external js files!
<html>
<body style="background-color:rgb(140,
192, 192);">
<script
src="../js/dateGetDayName.js"></script>
<script
src="../js/dateGetMonthName.js"></script>
<script
src="../js/dateDayMessage.js"></script>
<script
src="../js/dateMonthMessage.js"></script>
<script src="../js/showInfo.js"></script>
<button
onclick="showInfo('showDayMessage',
messageFromDay(), 'Hi')">Day
Message</button>
<hr>
<button
onclick="showInfo('showMonthMessage',
messageFromMonth(), 'Hi')">Month
Message</button>
<div id="showMonthMessage"
style="font-size:30pt">Day Message</div>
</body>
</html>
Planning Bigger Projects
When we are creating our library, we may want
to organize things by using an index.html file.
function ourSimpleFunction()
{
alert("Hi Everyone");
}
<html>
<head>
<title>Button Press</title>
<script src="../js/ourSimpleFunction.js">
</script>
</head>
<body>
</body></html>
<html><head><style>
a{
color: rgb(30, 99, 211);
font-size: 20pt;
}
</style></head>
<body style="background-color:rgb(140,
192, 192);">
<a href =
"./html/ourSimpleFunction.html">Our
Simple Function</a>
</body></html>
class mathFunctions
{
numberSquare(number)
{
return number * number;
}
}
<html>
<body style="background-color:rgb(140,
192, 192);">
<script
src="../js/mathNumberSquare.js"></script
>
<script src="../js/showInfo.js"></script>
<button onclick="showInfo('showResult',
ourMath.numberSquare(7), 'Result
is')">Press</button>
</body></html>
function mathNumberSquare(number)
{
return number * number;
}
uses as mathNumberSquare1.js
<html>
<body style="background-color:rgb(140,
192, 192);">
<script
src="../js/mathNumberSquare1.js"></scrip
t>
<script src="../js/showInfo.js"></script>
<button onclick="showInfo('showResult',
mathNumberSquare(7), 'Result
is')">Press</button>
</body>
</html>
Combine all js files into one
</script>
</head>
<body>
<input type = "button"
value = "Would you like google"
onclick = "goHere()">
</body>
</html>
ask and say name
<body>
<script>
function ourQuestion()
{
let name = prompt("Enter your name ","
");
alert(name);
}
ourQuestion();
</script>
</body>
</html>
date and time, simple
</head>
<body>
<input type = "button"
value = "Click for Time"
onclick = "ourTime( )">
</body>
</html>
do while prompt, enter name
</script>
</head>
<body>
<input type = "button"
value = "Enter Name"
onclick = "enterName()">
</body>
</html>
do while prompt, enter name
<html><head><title> do while locations
</title>
<script>
function pass2Go()
{
let pass;
do
{
pass = prompt("Enter Password");
} while (!pass);
if (pass == "789")
{
location.href =
"http://www.google.com";
}
else
{
location.href =
"http://www.yahoo.com";
}
}
</script>
</head>
<body>
<input type = "button"
value = "Enter Pass for Go site"
onclick = "pass2Go( )">
</body>
</html>
do while using and
</head>
<body>
if(check == true) {
alert("Tom teaches Physics");
}
}
function johnInfo( )
{
let check =
document.getElementById("johnCheck").c
hecked;
if(check == true) {
alert("John teaches Math ");
}
}
function janeInfo( )
{
let check =
document.getElementById("janeCheck").c
hecked;
if(check == true) {
alert("Jane teaches Science");
}
}
</script></head>
<body>
<input type = "checkbox"
value = "Toms information"
onclick = "tomInfo()" id="tomCheck">
Click to find out what Tom teaches
<script>
document.write(people.sort());
</script>
</body>
</html>
if many
<html><head><title> What is your age
</title>
<script>
function howold()
{
let age = prompt("Enter your age");
</body></html>
Enter Name, toLowerCase()
<html><head><title> Lower Case the Text
</title>
<script>
function ourLower()
{
let name = prompt("Enter your Name");
let lower = name.toLowerCase();
if (lower == "chris")
{
alert("Hi Chris");
}
else {
alert("Where is Chris?");
}
}
</script></head>
<body>
<input type = "button"
value = "Enter your Name"
onclick = "ourLower()">
</body></html>
combining strings
<html><head><title> Music Questions
</title>
<script>
function music()
{
let band = prompt("Favorite Band?");
let type = prompt("Favorite Music
Type?");
document.write(band +
" is great " +
type +
" music");
}
</script></head>
<body>
<script>
music();
</script>
</body></html>
combining strings prompt
<body>
<input type = "button"
value = "Say my name"
onclick = "askFullName()">
</body></html>
while count down
function countDown()
{
let count = 100;
while (count != 0) {
document.write(count + "<br>");
count--;
}
}
</script>
</head>
<body>
<input type = "button"
value = "Count down from 100"
onclick = "countDown()">
</body></html>
while count up
<body>
<input type = "button"
value = "Count to 100 from 1"
onclick = "countUp()">
</body></html>
quick math question
<html><body>
<script>
let myRecord = [
{
name: "John",
height: "5 Feet 10 Inches",
weight: 140,
},
{
name: "Amy",
height: "6 Feet 4 Inches",
weight: 160,
} ];
function showIt()
{
let theDiv =
document.getElementById("theDisplay");
theDiv.innerHTML =
JSON.stringify(myRecord);
}
</script>
<button id = "b1" onclick="showIt()">
Show Records
</button>
<div id = "theDisplay"></div>
</body></html>
show Records for loop and filter
<html><body><script>
let myRecord = [
{
name: "Debra",
year: 1977
},
{
name: "Alison",
year: 1978
} ];
function showRecord()
{
let theDiv =
document.getElementById("theDisplay");
let output = ' ';
for(let i = 0; i < myRecord.length; i++)
{
if(myRecord[ i ].year == 1977) {
output += myRecord[ i ].name +
'<br>';
}
}
theDiv.innerHTML = output;
}
</script>
<button id = "b1" onclick =
"showRecord( )"> Show </button>
<div id = "theDisplay"></div>
</body></html>
show Records for loop and filter
<html><body><script>
let myRecord = [
{
name: "Brandi",
year: 1980
},
{
name: "Melissa",
year: 1973
} ];
function showRecord(criteria)
{
let theDiv =
document.getElementById("theDisplay");
let output = ' ';
for(let i = 0; i < myRecord.length; i++)
{
if(myRecord[ i ].year < criteria) {
output += myRecord[ i ].name +
'<br>';
}
}
theDiv.innerHTML = output;
}
</script>
<html><body><script>
let myRecord = [
{
name: "Jessica",
year: 1980
},
{
name: "Kelly",
year: 1974
} ];
<html><body><script>
let myRecord = [
{
person: "Terry",
eyeColor: "Brown",
},
{
person: "Heidi",
eyeColor: "Blue",
}
];
function showIt()
{
let theDiv =
document.getElementById("display");
let output = '';
for(let i = 0; i < myRecord.length; i++)
{
output += myRecord[ i ].person + " has
";
output += myRecord[ i ].eyeColor + "
color eyes";
output += '<br>';
}
theDiv.innerHTML = output;
}
</script>
<button id="b1"
onclick="showIt()">Insert</button>
<div id="display"></div>
</body>
</html>
hour and minutes
<html><head><script>
function dateTime() { //GET DATE TIME
let date = new Date();
return date;
}
function Hours() { //GET HOURS
let theHours = dateTime().getHours();
return theHours;
}
<button onclick="displayIt('div1',
Hours());"> Hours </button>
<button onclick="displayIt('div2',
Minutes());"> Minutes </button>
<div id="div1"></div><div
id="div2"></div>
</body>
</html>
date month message
<html><head><script>
function months()
{
let date = new Date();
let month = date.getMonth();
// 0 to 11, 0 is jan , 11 is december
if(month == 0) { month = "January"; }
else if(month== 1) { month =
"February"; }
else if(month== 2) { month= "March"; }
else if(month== 3) { month= "April"; }
else if(month== 4) { month= "May"; }
else if(month== 5) { month= "June"; }
else if(month== 6) { month= "July"; }
else if(month== 7) { month= "August"; }
else if(month== 8) { month=
"September"; }
else if(month== 9) { month= "October";
}
else if(month== 10) { month=
"November"; }
else if(month== 11) { month=
"December"; }
return month;
}
function messageMonth(){
let text;
if(months() == "March") { text = "March.
Spring time."; }
else if(months() == "April") { text =
"April. Getting Warm"; }
else if(months() == "May") { text = "May.
Getting nicer"; }
else if(months() == "June") { text =
"June. Summer time"; }
else if(months() == "July") { text = "July.
Warm Summer"; }
else if(months() == "August") { text =
"August. Summer"; }
else if(months() == "September") { text =
"September here"; }
else if(months() == "October") { text =
"October. Cooler"; }
else if(months() == "November") { text =
"November. "; }
else if(months() == "December") { text =
"December. time"; }
else if(months() == "January") { text =
"January. Snowy"; }
else if(months() == "February") { text =
"February. Burrr"; }
document.getElementById("monthDisplay
").innerHTML = text;
}
</script></head>
<body onload="messageMonth();">
<div id="monthDisplay"></div>
</body></html>
hour message
<html><head><script>
function theHours()
{
let date = new Date();
let hours = date.getHours(); //0 to 23
return hours;
}
function messageHours()
{
let hoursMessage;
console.log(theHours());
if(theHours() >= 0 && theHours() < 6) {
hoursMessage = "Its Early Morning";
}
else if(theHours() >= 6 && theHours() <
12) {
hoursMessage = "Its Mid Morning"; }
else if(theHours() >= 12 && theHours() <
18) {
hoursMessage = "Its Afternoon"; }
else if(theHours() >= 18 && theHours()
<= 23) {
hoursMessage = "Its night time";
}
document.getElementById("hoursDisplay"
).innerHTML = hoursMessage;
}
</script></head>
<body onload="messageHours();">
<div id="hoursDisplay"></div>
</body></html>
class simple
<html><body><script>
class Person
{
constructor(firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
greet()
{
document.write("Name is " +
this.firstName + " " +
this.lastName);
}
}
<button onclick="displayIt('div1',
david.greet())">Hi</button>
<button onclick="displayIt('div2',
david.bye())">Bye</button>
<div id="div1"></div>
<div id="div2"></div>
</body></html>
class inheritance, extends
<html><head><script>
class Animal
{
constructor(kind, name, size, weight,
speed)
{
this.kind = kind;
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed + " mph";
}
eat() {
return 'eating';
}
saySpeed(){
return this.speed;
}
}
class Cat extends Animal
{
constructor(kind, name, size, weight,
speed, tailSize)
{
super(kind, name, size, weight, speed);
this.tailSize = tailSize;
}
meow(){
return 'meowing';
}
sayKind(){
return this.kind;
}
tailWag(){
return this.tailSize + ' tail wagging';
}
}
</script>
</head>
<body>
<button onclick="show('display',
felix.meow())">Meow</button>
<button onclick="show('display',
felix.eat())">Eat </button>
<button onclick="show('display',
felix.sayKind())">Kind
</button>
<button onclick="show('display',
felix.tailWag())">Wag</button>
<button onclick="show('display',
felix.saySpeed())">Speed
</button>
<div id="display"></div>
</body></html>
class inheritance, extends person
<html><head><script>
class Person
{
constructor(firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
greet(){
let theGreeting = "Howdy " +
this.firstName + " " + this.lastName;
return theGreeting;
}
bye() {
let theSalutation = "Bye " +
this.firstName + " " + this.lastName;
return theSalutation;
}
}
class Alien extends Person
{
constructor(firstName, lastName,
alienName)
{
super(firstName, lastName,
alienName);
this.alienName = alienName;
}
alienGreeting() {
let alienHi = "My name is not " +
this.firstName + " " +
this.lastName + ". " +
" My Real name is " +
this.alienName + ". " + " I am an alien
from another world.";
return alienHi;
}
}
function displayIt(where, whichOne)
{
document.getElementById(where).innerHT
ML = whichOne;
}
let superman = new Alien("Clark","Kent",
"Kal-El");
</script>
<body>
<button onclick="displayIt('div1',
superman.greet())">Greeting
</button>
<button onclick="displayIt('div2',
superman.bye())">Salutation
</button>
<button onclick="displayIt('div3',
superman.alienGreeting())">
Alien Greeting</button>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
convert, f to c
<html><head><script>
function f2c(f)
{
let centigrade;
centigrade = 5/9 * (f - 32);
return centigrade;
}
function convertButton()
{
let inputCentigrade =
document.getElementById("inputDegrees"
).value;
inputCentigrade = f2c(inputCentigrade);
document.getElementById("answer").text
Content = inputCentigrade;
}
</script></head>
<body>
<input type="text" id="inputDegrees"
value = "0.0">
<br>
</body>
</html>
class simple, and this
</script>
</head>
<body
onload="somethingToWrite.showText();">
</body></html>
keyboard control
<html><head>
<title>Keyboard Control</title>
<script>
function whenLoaded()
{
window.addEventListener("keydown",
whichKeyPressed, false);
}
function whichKeyPressed(event)
{
if(event.keyCode === 112)
{
alert("F1 Key pressed");
}
else if(event.keyCode === 27)
{
alert("Escape Key pressed");
}
else if(event.keyCode === 32)
{
alert("Spacebar pressed");
}
else if(event.keyCode === 65)
{
alert("The Letter a, was pressed");
}
}
</script></head>
<body onload="whenLoaded()">
<html><head><body>
<script>
let textToMove;
function whenLoads()
{
textToMove =
document.getElementById("theDiv");
textToMove.style.position='relative';
textToMove.style.left='0px';
textToMove.style.top='50px';
}
function moveLeft()
{
textToMove.style.left =
parseInt(textToMove.style.left)-8 + 'px';
}
function moveRight( )
{
textToMove.style.left =
parseInt(textToMove.style.left)+8 + 'px';
}
function moveUp()
{
textToMove.style.top =
parseInt(textToMove.style.top)-8 + 'px';
}
function moveDown()
{
textToMove.style.top =
parseInt(textToMove.style.top)+8 +
'px';
}
</script></head>
onmousedown="moveLeft( );"><
/button><br>
onmousedown="moveRight( );"><
/button><br>
<input type="button" id="upButton"
value="Up"
onmousedown="moveUp( );"></button>
<br>
</body>
</html>
saved as timeGetTime.js
function theHours()
{
let date = new Date();
let hours = date.getHours();
//0 to 23
if(hours == 0)
{
hours = 12; //am
}
else if(hours == 1)
{
hours = 1; //am
}
else if(hours == 2)
{
hours = 2; //am
}
else if(hours == 3)
{
hours = 3; //am
}
else if(hours == 4)
{
hours = 4; //am
}
else if(hours == 5)
{
hours = 5; //am
}
else if(hours == 6)
{
hours = 6; //am
}
else if(hours == 7)
{
hours = 7; //am
}
else if (hours == 8)
{
hours = 8; //am
}
else if(hours == 9)
{
hours = 9; //am
}
else if(hours == 10)
{
hours = 10; //am
}
else if(hours == 11)
{
hours = 11; //am
}
else if(hours == 12)
{
hours = 12; //pm
}
else if(hours == 13)
{
hours = 1; //pm
}
else if(hours == 14)
{
hours = 2; //pm
}
else if (hours == 15)
{
hours = 3; //pm
}
else if (hours == 16)
{
hours = 4; //pm
}
else if (hours == 17)
{
hours = 5; //pm
}
else if (hours == 18)
{
hours = 6; //pm
}
else if(hours == 19)
{
hours = 7; //pm
}
else if(hours == 20)
{
hours = 8; //pm
}
else if (hours == 21)
{
hours = 9; //pm
}
else if (hours == 22)
{
hours = 10; //pm
}
else if (hours == 23)
{
hours = 11; //pm
}
return hours;
}
function theMinutes()
{
let date = new Date();
let minutes = date.getMinutes();
if(minutes==0)
{
return minutes = "00";
}
else if(minutes == 1)
{
return minutes = "01";
}
else if(minutes == 2)
{
return minutes = "02";
}
else if(minutes == 3)
{
return minutes = "03";
}
else if(minutes == 4)
{
return minutes = "04";
}
else if(minutes == 5)
{
return minutes = "05";
}
else if(minutes == 6)
{
return minutes = "06";
}
else if(minutes == 7)
{
return minutes = "07";
}
else if(minutes == 8)
{
return minutes = "08";
}
else if(minutes == 9)
{
return minutes = "09";
}
return minutes;
}
function amOrpm()
{
let date = new Date();
let hours = date.getHours(); //0 to 23
let halfOfDay;
if(hours == 0)
{
halfOfDay = "am";
}
else if(hours == 1)
{
halfOfDay = "am";
}
else if(hours == 2)
{
halfOfDay = "am";
}
else if(hours == 3)
{
halfOfDay = "am";
}
else if(hours == 4)
{
halfOfDay = "am";
}
else if(hours == 5)
{
halfOfDay = "am";
}
else if(hours == 6)
{
halfOfDay = "am";
}
else if(hours == 7)
{
halfOfDay = "am";
}
else if(hours == 8)
{
halfOfDay = "am";
}
else if(hours == 9)
{
halfOfDay = "am";
}
else if (hours == 10)
{
halfOfDay = "am";
}
else if(hours == 11)
{
halfOfDay = "am";
}
else if(hours == 12)
{
halfOfDay = "pm";
}
else if(hours == 13)
{
halfOfDay = "pm";
}
else if(hours == 14)
{
halfOfDay = "pm";
}
else if(hours == 15)
{
halfOfDay = "pm";
}
else if(hours == 16)
{
halfOfDay = "pm";
}
else if(hours == 17)
{
halfOfDay = "pm";
}
else if(hours == 18)
{
halfOfDay = "pm";
}
else if(hours == 19)
{
halfOfDay = "pm";
}
else if(hours ==20)
{
halfOfDay = "pm";
}
else if(hours == 21)
{
halfOfDay = "pm";
}
else if (hours == 22)
{
halfOfDay = "pm";
}
else if (hours == 23)
{
halfOfDay = "pm";
}
return halfOfDay;
}
function theSeconds()
{
let date = new Date();
let seconds = date.getSeconds();
if(seconds==0)
{
return seconds = "00";
}
else if(seconds == 1)
{
return seconds = "01";
}
else if(seconds == 2)
{
return seconds = "02";
}
else if(seconds == 3)
{
return seconds = "03";
}
else if(seconds == 4)
{
return seconds = "04";
}
else if(seconds == 5)
{
return seconds = "05";
}
else if(seconds == 6)
{
return seconds = "06";
}
else if(seconds == 7)
{
return seconds = "07";
}
else if(seconds == 8)
{
return seconds = "08";
}
else if(seconds == 9)
{
return seconds = "09";
}
return seconds;
}
function timeKeepGettingTime()
{
timeFunctionUpdates();
t=
setInterval('timeFunctionUpdates()',500);
}
function timeFunctionUpdates()
{
document.getElementById("showTime").in
nerHTML = theHours() + ":" + theMinutes()
+ ":" + theSeconds() + " " + amOrpm();
}
<html>
<body style="background-color:rgb(140,
192, 192);">
<script
src="../js/timeGetTime.js"></script>
<button
onclick="timeKeepGettingTime();">Time</
button>
</body>
</html>
function messageHours()
{
//console.log(theHours());
if(theHours() >= 0 && theHours() < 6) {
return "It's Early Morning.";
}
else if(theHours() >= 6 && theHours() <
12) {
return "It's Mid Morning.";
}
else if(theHours() >= 12 && theHours() <
18) {
return "It's Afternoon.";
}
else if(theHours() >= 18 && theHours()
<= 23) {
return "It's night time.";
}
}
function displayTimeMessage()
{
document.getElementById("hoursDisplay"
).innerHTML = messageHours();
}
<button
onclick="displayTimeMessage();">Time
Message</button>
</body>
</html>
CollegeOfScripting.wordpress.com
Youtube.com/ScriptingCollege
Github.com/ChristopherTopalian
Dedicated to God the Father
This book is created by the
College of Scripting Music & Science
Always remember, that each time you write a
script with a pencil and paper, it becomes
imprinted so deeply in memory that the
material and methods are learned extremely
well.
When you Type the scripts, the same is true.
The more you type and write out the scripts
by keyboard or pencil and paper, the more
you will learn programming!
Write and Type EVERY example that you find.
Keep all of your scripts organized.
Every script that you create increases your
programming abilities.
SEEING CODE, is one thing,
but WRITING CODE is another.
Write it, Type it, Speak It, See It, Dream It.
www.CollegeOfScripting.weebly.com