Created
November 21, 2019 18:30
-
-
Save parkerfoshay/4039ba02475bb9f79283c79e754fd553 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 "./App.css"; | |
function App() { | |
const [fibonacci, setFibonacci] = useState(null); | |
const [value, setValue] = useState(null); | |
const [initialNumber, setInitialNumber] = useState(null); | |
function fibonacciHelper(number) { | |
if (number < 2) { | |
return number; | |
} | |
return fibonacciHelper(number - 1) + fibonacciHelper(number - 2); | |
} | |
function submitHandler(event) { | |
event.preventDefault(); | |
setFibonacci(fibonacciHelper(value)); | |
setValue(null); | |
} | |
function inputHandler(event) { | |
event.preventDefault(); | |
setValue(event.target.value); | |
setInitialNumber(event.target.value); | |
setFibonacci(null); | |
} | |
return ( | |
<div className="App"> | |
<form action="" onSubmit={submitHandler}> | |
<input type="number" onChange={inputHandler} /> | |
<button>fibonacci</button> | |
</form> | |
<h3> | |
the fibonacci of {initialNumber} is {fibonacci} | |
</h3> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment