It looks like you're building a simple calculator in CodeSandbox with HTML, JavaScript, and CSS. Here's a guide on how to implement the necessary additions and changes based on your description.
Final HTML Code:
Make sure your index.html
file looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form>
First number: <input type="text" id="number1" /><br><br>
Second number: <input type="text" id="number2" /><br><br>
<button type="button" onClick="add()">Add</button>
<button type="button" onClick="subtract()">Subtract</button>
<button type="button" onClick="multiply()">Multiply</button>
<button type="button" onClick="divide()">Divide</button>
</form>
<p>The answer is: </p>
<div id="answer"></div>
</body>
</html>
Final JavaScript Code:
Your index.js
file should contain these functions:
function add(){
var num1 = document.getElementById("number1").value;
var num2 = document.getElementById("number2").value;
num1 = parseInt(num1);
num2 = parseInt(num2);
document.getElementById("answer").innerHTML = num1 + num2;
}
function subtract(){
var num1 = document.getElementById("number1").value;
var num2 = document.getElementById("number2").value;
num1 = parseInt(num1);
num2 = parseInt(num2);
document.getElementById("answer").innerHTML = num1 - num2;
}
// Make sure you have these functions defined as well
function multiply(){
var num1 = document.getElementById("number1").value;
var num2 = document.getElementById("number2").value;
num1 = parseInt(num1);
num2 = parseInt(num2);
document.getElementById("answer").innerHTML = num1 * num2;
}
function divide(){
var num1 = document.getElementById("number1").value;
var num2 = document.getElementById("number2").value;
num1 = parseInt(num1);
num2 = parseInt(num2);
if (num2 !== 0) {
document.getElementById("answer").innerHTML = num1 / num2;
} else {
document.getElementById("answer").innerHTML = "Cannot divide by zero";
}
}
Final CSS Code:
Your CSS file (style.css
) should look something like this:
body {
color: green;
font-weight: bold;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
background-color: beige;
}
input {
width: 10%;
padding: 10px 10px;
}
button {
background-color: green;
color: white;
border: 2px solid brown;
padding: 10px 10px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: darkgreen;
}
#answer {
font-size: 18px;
font-weight: bold;
color: red;
}
Testing and Shortcut Creation:
- After you've added and updated your files, click the buttons with different inputs to test the functionality of addition and subtraction.
- If the addition still produces incorrect output, double-check that you are using
parseInt()
correctly on both input numbers. - To create a shortcut, drag the URL of your CodeSandbox to your desktop and rename it.
Submitting Your Work:
- Once everything is working, click on the Share button in CodeSandbox, and copy the sandbox link.
- Provide a screenshot of the desktop shortcut you created as part of your submission.
If you encounter any problems during your process, explain the specific issues in your submission and what you've attempted to resolve them. Happy coding!