Last active
March 21, 2019 22:09
-
-
Save shamaru001/48064bdc41efd0da12e3dd043304a8b0 to your computer and use it in GitHub Desktop.
Example Component made on ReactJs. this component make a Fibonacci sequence.
This file contains hidden or 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 ReactDOM from 'react-dom'; | |
import React, { Component } from 'react'; | |
// Example code to reproduce the fibonnacci sequence | |
// By: Shamaru Primera <[email protected]> | |
class App extends Component { | |
constructor(props){ | |
super(props) | |
this.state = { | |
natural_set: [] | |
} | |
} | |
fibo(){ | |
let natural_set = this.state.natural_set; | |
if (natural_set.length == 0){ | |
natural_set = [0] | |
this.setState({ | |
natural_set | |
}); | |
} | |
else{ | |
let first = natural_set[natural_set.length +(-1)]; | |
let second = natural_set[natural_set.length +(-2)] || 0; | |
let plus = (first + second) || 1; | |
natural_set.push(plus) | |
this.setState({ | |
natural_set | |
}) | |
} | |
} | |
render(){ | |
return ( | |
<div> | |
<ul>{this.state.natural_set.map( (n) => <li>{n}</li>)}</ul> | |
<h3 onClick={this.fibo.bind(this)}>CLICK ME</h3> | |
</div> | |
) | |
} | |
} | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment