Last active
August 18, 2020 14:08
-
-
Save Mayankgupta688/029bada6b98a234d5268d905ca8fc042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import "./styles.css"; | |
export default function App() { | |
var [numerator, setNumerator] = useState(""); | |
var [denominator, setDenominator] = useState(""); | |
var [executionOutput, setExecutionOutput] = useState(""); | |
var [hasError, setHasError] = useState(false); | |
function getDivision() { | |
try { | |
if (denominator === "0") { | |
throw new Error("Division By 0"); | |
} | |
setExecutionOutput(numerator / denominator); | |
} catch { | |
setHasError(true); | |
} | |
} | |
function updateValue(event) { | |
if (event.target.id === "numerator") { | |
setNumerator(event.target.value); | |
} else { | |
setDenominator(event.target.value); | |
} | |
} | |
return ( | |
<div> | |
{!hasError && ( | |
<section className="App"> | |
<div> | |
<label>First Value:{" "}</label> | |
<input id="firstValue" type="text" value={numerator} onChange={updateValue} /> | |
</div> | |
<div> | |
<label>Second Value:{" "}</label> | |
<input id="secondValue" type="text" value={denominator} onChange={updateValue} /> | |
</div> | |
<div>Output: {executionOutput}</div> | |
<input type="button" onClick={getDivision} value="Divide Values" /> | |
</section> | |
)} | |
{hasError && <ErrorComponent></ErrorComponent>} | |
</div> | |
); | |
} | |
function ErrorComponent() { | |
return <h1>Division by 0 Error</h1> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment