ASDV-WebDev/Semester 1/Assignments/JavaScript/MP13_CalebFontenot/public_html/Problem7.html
2023-08-16 17:31:33 -05:00

57 lines
1.9 KiB
HTML

<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this template
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
table, th, td {
border: 1px solid;
}
</style>
<body>
<div id="tableDiv"></div>
<div id="magicResult"></div>
</body>
<script>
// Create 2D array.
let magic = [];
for (let i = 0; i < 4; ++i) {
magic.push([0, 0, 0, 0]);
}
console.log(magic);
//Get input
for (let x = 0; x < magic.length; ++x) {
for (let y = 0; y < magic[x].length; ++y) {
magic[x][y] = prompt("Enter value for " + (x + 1) + ", " + (y + 1));
}
}
console.log(magic);
// Calculate diagonals
var diagonal1 = magic[0][0] + magic[1][1] + magic[2][2] + magic[3][3];
var diagonal2 = magic[0][3] + magic[1][2] + magic[2][1] + magic[3][0];
let tableString = "<table>";
for (let x = 0; x < magic.length; ++x) {
tableString += "<tr>";
for (let y = 0; y < magic[x].length; ++y) {
tableString += "<td>" + magic[x][y] + "</td>";
}
tableString += "</tr>";
}
tableString += "</table>";
let magicResult = "";
if (diagonal1 == diagonal2) {
magicResult = "This is a magic square.";
} else {
magicResult = "This is not a magic square."
}
document.getElementById("tableDiv").innerHTML = tableString;
document.getElementById("magicResult").innerHTML = "<p>" + magicResult + "</p>";
</script>
</html>