Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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: []
- }
- }
- fibo(){
- let natural = this.state.natural;
- if (natural.length == 0){
- natural = [1]
- this.setState({
- natural
- });
- }
- else{
- let first = natural[natural.length +(-1)];
- let second = natural[natural.length +(-2)] || 0;
- let plus = first + second;
- natural.push(plus)
- this.setState({
- natural
- })
- }
- }
- render(){
- return (
- <div>
- <ul>{this.state.natural.map( (n) => <li>{n}</li>)}</ul>
- <h3 onClick={this.fibo.bind(this)}>CLICK ME</h3>
- </div>
- )
- }
- }
- export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement