Skip to content

Commit 0ed99b3

Browse files
added calculator
1 parent 320337a commit 0ed99b3

1 file changed

Lines changed: 50 additions & 33 deletions

File tree

javascript.html

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ <h2>Workshop</h2>
2121
</li>
2222
<li><a href="#dom">DOM & Interactivity</a></li>
2323
<li><a href="#events">Events</a></li>
24+
<li><a href="#forms">Forms</a></li>
25+
<li><a href="#element_loop">Element Loop</a></li>
2426

2527
</ul>
2628
</nav>
@@ -428,13 +430,15 @@ <h2>The DOM: Connecting JavaScript to HTML</h2>
428430

429431
<section id="events">
430432
<h2>Events & User Interaction</h2>
431-
<p>Events allow your page to respond to user actions such as clicks or typing.</p>
432-
433+
<p>Events allow your page to respond to user actions such as clicks or typing.
434+
The following example shows how to show/hide an element:
435+
</p>
436+
<h3>HTML</h3>
433437
<pre><code>&lt;button id="toggle"&gt;Show Abstract&lt;/button&gt;
434438
&lt;p id="abstract" hidden&gt;
435439
This project examines...
436440
&lt;/p&gt;</code></pre>
437-
441+
<h3>JavaScript</h3>
438442
<pre><code>const button = document.querySelector("#toggle");
439443
const abstract = document.querySelector("#abstract");
440444

@@ -444,39 +448,52 @@ <h2>Events & User Interaction</h2>
444448

445449
</section>
446450

447-
<section id="highlight_active">
448-
<h2>Highlighting and Active Selection</h2>
449-
<p>Adding and removing CSS classes can help draw attention to elements</p>
451+
<section id="forms">
452+
<h2>Forms</h2>
453+
<p>Accepting user input is often handled using a form element.
454+
The following example shows how to create a simple calculator using html and javascript.</p>
455+
<h3>HTML</h3>
456+
<pre><code id="code-container0"></code></pre>
457+
<script>
458+
// The raw HTML code you want to display
459+
var rawHTML = `
460+
<form onsubmit="calculate(event)">
461+
<label for="equation">Enter an equation:</label><br>
462+
<input type="text" id="equation" placeholder="e.g. 2 + 3 * 4">
463+
<button type="submit">Calculate</button>
464+
</form>
450465
451-
<p>The following listens to the scrolling event and highlights the section that has been scrolled into view</p>
452-
<h3>CSS</h3>
453-
<pre><code>
454-
section.active {
455-
border-left: 4px solid #2563eb;
456-
padding-left: 1rem;
457-
background-color: #f1f5f9;
458-
}
459-
</code></pre>
460-
<h3>JavaScript</h3>
461-
<pre><code>const sections = document.querySelectorAll("section");
462-
463-
function highlightSection() {
464-
sections.forEach(section => {
465-
const rect = section.getBoundingClientRect();
466-
467-
if (rect.top >= 0 && rect.top < 200) {
468-
section.classList.add("active");
469-
} else {
470-
section.classList.remove("active");
471-
}
472-
});
473-
}
466+
<p><strong>Result:</strong> <span id="result"></span></p>
467+
`;
474468

475-
window.addEventListener("scroll", highlightSection);</code></pre>
469+
// Set the textContent property to display the code as text
470+
document.getElementById('code-container0').textContent = rawHTML;
471+
</script>
472+
<h3>JavaScript</h3>
473+
<pre><code>
474+
function calculate(event) {
475+
event.preventDefault(); // stop form from refreshing page
476+
477+
const equation = document.getElementById("equation").value;
478+
const resultEl = document.getElementById("result");
479+
480+
// Note: eval() is dangerous in real apps.
481+
// Expressions should be sanitized before processing to prevent injecting Javascript.
482+
try {
483+
const answer = eval(equation);
484+
resultEl.textContent = answer;
485+
} catch (error) {
486+
resultEl.textContent = "Invalid expression";
487+
}
488+
}</code></pre>
476489

490+
</section>
477491

478-
<h3>Looping through DOM elements</h3>
479-
492+
<section id="element_loop">
493+
494+
<h2>Looping through DOM elements</h2>
495+
<p>It may be beneficial to loop through all the elements on your webpage.
496+
Here's how you can accomplish this:</p>
480497
<pre><code>
481498
const buttons = document.querySelectorAll("button");
482499

@@ -487,7 +504,7 @@ <h3>Looping through DOM elements</h3>
487504
});
488505
</code></pre>
489506

490-
507+
More information on <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll">querySelectorAll</a>
491508
</section>
492509

493510
<footer>

0 commit comments

Comments
 (0)