Skip to content

Instantly share code, notes, and snippets.

@shamaru001
Last active March 21, 2019 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamaru001/48064bdc41efd0da12e3dd043304a8b0 to your computer and use it in GitHub Desktop.
Save shamaru001/48064bdc41efd0da12e3dd043304a8b0 to your computer and use it in GitHub Desktop.
Example Component made on ReactJs. this component make a Fibonacci sequence.
import ReactDOM from 'react-dom';
import React, { Component } from 'react';
// Example code to reproduce the fibonnacci sequence
// By: Shamaru Primera <shamaru001@gmail.com>
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