<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 4</title>
<style>
#notify { color: red; display: none; }
</style>
<script>
function checkSubmit(e) {
var fname = document.getElementById("fname").value;
var notification = document.getElementById("notify");
if (fname == "") {
notification.style.display = "block"
e.preventDefault( );
}
else {
notification.style.display = "none";
}
}
function init( ) {
document.getElementById("form1").addEventListener("submit",
checkSubmit);
}
window.addEventListener("load", init);
</script>
</head>
<body>
<h1>Exercise 4</h1>
<form id="form1"
action="http://ectweb.cs.depaul.edu/sjost/receipt.php"
method="POST">
<label for="fname">First Name:</label><br>
<input type="text" id="fname" name="fname"><br>
<span id="notify">First name is a required field<br>.</span><br>
<input type="submit" value="Submit Form">
</form>
</body>
</html>
<form action="https:www.server.com/receipt.php" method="get">
<input type="text" placeholder="Enter your name:"><br>
<input type="number" placeholder="Enter your age:">
</form>
Answer: both the name and age fields must have name attributes for these
values to be sent to the server.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Square Root Calculator</title>
<script>
window.onload = ( ) => {
document.querySelector("#btn1").onclick = ( ) => {
var x = document.querySelector("#in1").value;
var output = document.querySelector("#out1");
output.value = Math.sqrt(x);
};
};
</script>
</head>
<body>
<h1>Square Root Calculator</h1>
<input type="number" step="any" id="in1"><br><br>
<button id="btn1">Compute Square Root</button>
<input type="text" id="out1">
</body>
</html>
setInterval(callbackFunction, numMillisecs);
var a = [81]; a[4] = 37; document.writeln(a); document.writeln(a[2]); // Ans: 81,,,,37 undefined
function printArray(a) {
document.writeln(a);
}
var a = [2, 3, 5, 7, 11];
printArray(a);
// Output:
2,3,5,7,11
var a = [5, 3, 11, 2, 13, 7]; a.sort((s, t) => s - t); document.writeln(a); // Output 2,3,5,7,11,13
var a2d = [[5, 7, 2, 0],
[9, 3, 1, 7],
[2, 5, 3, 0],
[1, 8, 4, 2]];
document.writeln(a2d[1][3] + "<br>"); document.writeln(a2d[1] + "<br>"); document.writeln(a2d); // Output 7 9,3,1,7 5,7,2,0,9,3,1,7,2,5,3,0,1,8,4,2
function make2dArray(size) {
var arr = [ ];
for(let i = 0; i < size; i++) {
arr.push([0]);
for(let j = 0; j < size; j++) {
arr[i][j] = 0;
}
}
return arr;
}
Math.floor(Math.random( ) * 2);to generate random 0s and 1s with equal probability.
function make2dRandomArray(size) {
var arr = [ ];
for(let i = 0; i < size; i++) {
arr.push([0]);
for(let j = 0; j < size; j++) {
arr[i][j] = Math.floor(Math.random( ) * 2);
}
}
return arr;
}
function print2dArray(arr) {
var len = arr.length;
for(let i = 0; i < len; i++) {
for(let j = 0; j < len; j++) {
document.write(arr[i][j]);
}
document.writeln("<br>");
}
}
// Output:
111111110010010
100001111111001
010000011010101
100101100010101
100110011010000
000100101000110
001100101011000
010010110110001
011110000011001
111010000111010
101000011111100
010101100010111
000110011111110
110000100011110
000010010000000
00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000101100000000000 00011001110000000000 00011101100000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000In this array, 1 = alive and 0 = dead or non-existant. Here is the array represented with spaces (dead) and Xs (alive) instead of zeros and ones:
X XX
XX XXX
XXX XX
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="500" height="500"
style="border:3px solid #000000;">
Your browser does not support the canvas element.
</canvas>
<p id="out"></p>
<script>
var p = document.getElementById("out");
function make2dArray(size) {
var arr = [ ];
for(let i = 0; i < size; i++) {
arr.push([0]);
for(let j = 0; j < size; j++) {
arr[i][j] = 0;
}
}
return arr;
}
function print2dArray(arr) {
var len = arr.length;
for(let i = 0; i < len; i++) {
for(let j = 0; j < len; j++) {
document.write(arr[i][j]);
}
document.writeln("<br>");
}
}
const size = 100;
var a = make2dArray(size);
// Seed 2d array with random 0, 1 values.
for(let i = 1; i < size - 1; i++) {
for(let j = 1; j < size - 1; j++) {
a[i][j] = Math.random( ) >= 0.5 ? 1 : 0;
}
}
//document.writeln("<br><br>");
//print2dArray(a);
// Display grid from array values
function displayGrid( ) {
const cellSize = 5;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
for(var j = 1; j < size - 1; j++) {
var y = cellSize * j;
for(var i = 1; i < size - 1; i++) {
x = cellSize * i;
ctx.fillStyle = a[i][j] ? "#800000" : "#FFFFFF";
ctx.fillRect(x, y, cellSize, cellSize);
}
}
}
displayGrid( );
var b = make2dArray(size);
var n = 0;
function computeNextGeneration( ) {
for(let i = 1; i < size - 1; i++) {
for(let j = 1; j < size - 1; j++) {
let sum = a[i][j-1] + a[i][j+1] +
a[i-1][j-1] + a[i-1][j] + a[i-1][j+1] +
a[i+1][j-1] + a[i+1][j] + a[i+1][j+1];
if (sum < 2) {
b[i][j] = 0;
}
else if(sum == 2) {
b[i][j] = a[i][j];
}
else if(sum == 3) {
b[i][j] = 1;
}
else {
b[i][j] = 0;
}
}
}
// Copy next generation into array a.
for(let i = 1; i < size - 1; i++) {
for(let j = 1; j < size - 1; j++) {
a[i][j] = b[i][j];
}
}
displayGrid( );
p.innerHTML = "n = " + n;
n++;
}
setInterval(computeNextGeneration, 1000);
</script>
</body>
</html>
------------------------------------------
<!DOCTYPE html>
<!-- SlideInOut Example
Source code file: styles.css
Lorem ipsum paragraph slides in or out depending on button clicked. -->
<html lang="en">
<head>
<title>SlideInOut Example</title>
<script src="script.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Click buttons to slide paragraph in or out. -->
<p>
<button id="btnin">Slide In</button>
<button id="btnout">Slide Out</button>
</p>
<p id ="p1">
Lorem ipsum dolor sit amet, consectetur
....
vulputate tempor magna.
</p>
</body>
</html>
------------------------------------------
/* Source code file: styles.css */
body { font-family: 150%;
background-color: #E0E0FF; }
#p1 {
background-color: #A0A0FF;
overflow: hidden; }
------------------------------------------
// SlideInOut Example
// Source code file: styles.css
// Lorem ipsum paragraph slides in
// or out depending on button clicked.
var timer = null;
function slideOut( ) {
var divTag = document.getElementById("p1");
var percent = 100;
var ht = 110;
clearInterval(timer);
timer = setInterval(decreaseHeight, 25);
function decreaseHeight( ) {
if (percent <= 0) {
clearInterval(timer);
}
else {
percent -= 5;
divTag.style.height = percent * 0.01 * ht + "px";
}
}
}
function slideIn( ) {
var divTag = document.getElementById("p1");
var percent = 0;
var ht = 110;
clearInterval(timer);
timer = setInterval(increaseHeight, 25);
function increaseHeight( ) {
if (percent >= 100) {
clearInterval(timer);
}
else {
percent += 5;
divTag.style.height =
percent * 0.01 * ht + "px";
}
}
}
function init( ) {
var button1 = document.getElementById("btnout");
button1.addEventListener("click", slideOut);
var button2 = document.getElementById("btnin");
button2.addEventListener("click", slideIn);
}
window.addEventListener("load", init);
------------------------------------------
/* Here is the CSS file styles.css
Use the same HTML file as previous
problem without the buttons.
No JavaScript needed */
body { font-size: 150%;
font-family: Verdana;
background-color: #E0E0FF;
}
#p1 { overflow: hidden;
animation-duration: 1.0s;
animation-name: slidein;
}
@keyframes slidein {
from {
height: 0px;
}
to {
height: 250px;
}
}
<!DOCTYPE html>
<!-- FadeInOut Example
Source code file: styles.css
Lorem ipsum paragraph fades in or out depending on button clicked. -->
<html lang="en">
<head>
<title>FadeInOut Example</title>
<script src="script.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body id="b">
<!-- Click buttons to fade
paragraph in or out. -->
<p>
<button id="btnin">Fade In</button>
<button id="btnout">Fade Out</button>
</p>
<p id ="p1">
Lorem ipsum dolor sit amet, consectetur
...
vulputate tempor magna.
</p>
</body>
</html>
--------------------------------------------------
/* Source code file: styles.css */
body { font-family: 150%;
background-color: #E0E0FF; }
--------------------------------------------------
// Source code file: styles.css
var timer = null;
function fadeOut( ) {
var doc = document.getElementById("b");
var percent = 100;
var redComponent = 224;
var greenComponent = 224;
var blueComponent = 255;
clearInterval(timer);
timer = setInterval(decreaseBrightness, 25);
function decreaseBrightness( ) {
if (percent <= 0) {
clearInterval(timer);
}
else {
percent -= 5;
var rDec = Math.round(redComponent * percent * 0.01);
var gDec = Math.round(greenComponent * percent * 0.01);
var bDec = Math.round(blueComponent * percent * 0.01);
var rHex = rDec.toString(16).padStart(2, "0");
var gHex = gDec.toString(16).padStart(2, "0");
var bHex = bDec.toString(16).padStart(2, "0");
var hexColor = `#${rHex}${gHex}${bHex}`;
doc.style.backgroundColor = hexColor;
}
}
}
function fadeIn( ) {
var doc = document.getElementById("b");
var percent = 0;
var redComponent = 224;
var greenComponent = 224;
var blueComponent = 255;
clearInterval(timer);
timer = setInterval(increaseBrightness, 25);
function increaseBrightness( ) {
if (percent >= 100) {
clearInterval(timer);
}
else {
percent += 5;
var rDec = Math.round(redComponent * percent * 0.01);
var gDec = Math.round(greenComponent * percent * 0.01);
var bDec = Math.round(blueComponent * percent * 0.01);
var rHex = rDec.toString(16).padStart(2, "0");
var gHex = gDec.toString(16).padStart(2, "0");
var bHex = bDec.toString(16).padStart(2, "0");
var hexColor = `#${rHex}${gHex}${bHex}`;
doc.style.backgroundColor = hexColor;
}
}
}
function init( ) {
var button1 = document.getElementById("btnout");
button1.addEventListener("click", fadeOut);
var button2 = document.getElementById("btnin");
button2.addEventListener("click", fadeIn);
}
window.addEventListener("load", init);