Back

IT-WST02

Laboratory 5

EasyLaboratory3 files
0 visits

score: 100/100

directions

  • Create an HTML structure that includes an input field for the radius, a button to trigger the calculation, and a place to display the result. Save the file using the “Laboratory5.html” file name.
  • Create a separate file for JavaScript to handle the logic for calculating the area of a circle. Save this file as “script.js” and link it to your HTML file.
  • Add some basic styling to make the page visually appealing. Add the style using embedded stylesheet.
  • Refer to the example in the next slide.
  • Submit the entire document under a single folder named "Lab5_[your full name]" on our Google Classroom.

file structure

GeistVF.woff
Laboratory5.html
script.js

code

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Circle Area Calculator</title>
    <style type="text/css">
      :root {
        --card: 255, 255, 255;
        --foreground: 10, 10, 10;
        --border: 230, 230, 230;
        --radius: 0.5rem;
      }

      @font-face {
        font-family: "Geist";
        src: url("./font/GeistVF.woff") format("woff2");
      }

      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        font-family: "Geist", sans-serif;
        height: 50vh;
        background-color: #f7df1e;
        color: rgb(var(--foreground));
      }

      .container {
        background-color: #fff;
        border: 1px solid rgb(var(--border));
        box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        border-radius: var(--radius);
        padding: 2rem;
        margin: 5rem auto;
        width: fit-content;
        text-align: center;
      }

      .input-group {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 1rem;
        margin: 1rem 0;
      }

      label {
        font-size: 1rem;
      }

      input {
        display: flex;
        font-size: 0.875rem;
        width: auto;
        height: 2.25rem;
        padding: 0.25rem 0.75rem;
        border-radius: calc(var(--radius) - 2px);
        border: 1px solid rgb(var(--border));
        background-color: transparent;
        box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
      }

      button {
        font-weight: 500;
        color: rgb(var(--card));
        background-color: rgb(var(--foreground));
        padding: 0.5rem 1rem;
        border: none;
        border-radius: calc(var(--radius) - 2px);
        cursor: pointer;
      }

      button:hover,
      button:focus {
        background-color: rgb(var(--foreground), 0.8);
      }

      .result {
        font-weight: 500;
        margin-top: 1rem;
        font-size: 0.875rem;
        height: 0.875rem;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>
        <svg
          role="img"
          viewBox="0 0 24 24"
          xmlns="http://www.w3.org/2000/svg"
          style="width: 1em; height: 1em"
          fill="#f7df1e"
        >
          <title>JavaScript</title>
          <path
            d="M0 0h24v24H0V0zm22.034 18.276c-.175-1.095-.888-2.015-3.003-2.873-.736-.345-1.554-.585-1.797-1.14-.091-.33-.105-.51-.046-.705.15-.646.915-.84 1.515-.66.39.12.75.42.976.9 1.034-.676 1.034-.676 1.755-1.125-.27-.42-.404-.601-.586-.78-.63-.705-1.469-1.065-2.834-1.034l-.705.089c-.676.165-1.32.525-1.71 1.005-1.14 1.291-.811 3.541.569 4.471 1.365 1.02 3.361 1.244 3.616 2.205.24 1.17-.87 1.545-1.966 1.41-.811-.18-1.26-.586-1.755-1.336l-1.83 1.051c.21.48.45.689.81 1.109 1.74 1.756 6.09 1.666 6.871-1.004.029-.09.24-.705.074-1.65l.046.067zm-8.983-7.245h-2.248c0 1.938-.009 3.864-.009 5.805 0 1.232.063 2.363-.138 2.711-.33.689-1.18.601-1.566.48-.396-.196-.597-.466-.83-.855-.063-.105-.11-.196-.127-.196l-1.825 1.125c.305.63.75 1.172 1.324 1.517.855.51 2.004.675 3.207.405.783-.226 1.458-.691 1.811-1.411.51-.93.402-2.07.397-3.346.012-2.054 0-4.109 0-6.179l.004-.056z"
          />
        </svg>
        Welcome to the JavaScript World!!
      </h1>
        <div class="input-group">
          <label for="radius">Enter radius: </label>
          <input
            type="number"
            id="radius"
            min="1"
            required
          />
        </div>
        <button
          type="submit"
          onclick="calculateArea()"
        >
          Calculate Area
        </button>
      <div
        class="result"
        id="result"
      ></div>
    </div>
    <script src="script.js"></script>
  </body>
</html>
function calculateArea() {
	const radius = document.getElementById("radius").value;
	const result = document.getElementById("result");
	const area = Math.PI * radius * radius;

	if (radius === "") {
		result.innerHTML = "<span style='color: red; font-weight: 500;'>Input is empty</span>";
	} else if (radius <= 0) {
		result.innerHTML = "<span style='color: red; font-weight: 500;'>Radius must be greater than zero</span>";
	} else {
		result.innerHTML = "Area: " + "<span style='text-decoration: underline; text-underline-offset: 4px;'>" + area + "</span>";
	}
}

output

Laboratory 5