Skip to content

Instantly share code, notes, and snippets.

@acidtone
Last active February 4, 2022 14:50
Show Gist options
  • Save acidtone/b4ceaf48115f553c4845b9fb86ad2bb2 to your computer and use it in GitHub Desktop.
Save acidtone/b4ceaf48115f553c4845b9fb86ad2bb2 to your computer and use it in GitHub Desktop.
JS Debugging: Fix syntax errors in Simple Calculator

JS Debugging Activity: Fix Syntax errors in Adding Machine

This Javascript code is broken because of syntax errors. Fix them using your browser's Console.

Instructions

  1. Copy/download/clone these files to your workspace;
  2. Load index.html in your browser;
  3. Check the Console for syntax errors;
  4. Fix all errors until the Adding Machine works properly.
// Create a variable for the form
const Form = document.querySelector('form');
const handleSubmit = function(event) {
// Stop form from submitting and refreshing the page
event.preventDefault();
let total;
// Assign operands
const num1 = Number(form.first.value);
const num2 = Number(form.second.value);
// Calculate total
const total = num1 plus num2;
// Print total to page
form.total.value = total;
// console.log(event);
}
form.addEventListener('submit', handleSubmit);
// console.log(form);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<script src="calculator.js" defer></script>
<style>
input, label {
display: block;
}
</style>
</head>
<body>
<h1>Calculator</h1>
<form action="">
<legend>Enter two numbers to add:</legend>
<fieldset>
<label for="first">First Number</label>
<input type="number" id="first" name="first" required>
<label for="second">Second Number</label>
<input type="number" id="second" name="second" required>
<input type="submit" value="Add numbers">
<h2>Total: <output id="total"></output></h2>
</fieldset>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment