React
by guydog28
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
display: 8px 0;
}
.comma-list li {
display: inline;
}
.comma-list li::after {
content: ", ";
}
.comma-list li:last-child::after {
content: "";
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
/**
* This project has three tasks:
*
* 1. Implement the fibonacci and fibonacciArray methods to
* do as its jsdoc describes it should do. Replace
* placeholder return statements with your implementation.
* Click run at the top left to test your code output in the
*. window to the right.
* 2. Ensure application works as expected - if not fix any bugs
* 3. Change the display of the list to display the numbers on a
* single line, as a comma-separated list. Do not modify the
* structure of the html - modify attributes and/or CSS only.
*
**/
class FibonacciApp extends React.Component {
constructor(props) {
super(props)
}
/**
* Returns the nth number in the fibonacci sequence, where
* n is provided as a parameter.
*
* The first two numbers in the fibonacci sequence are 0 and
* 1. Each subsequent number is the sum of the previous two
* numbers
*
* Given this, the first 8 numbers then would be:
* 0, 1, 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), 13 (5+8)
*
* This method only returns the number requested. So:
* fibonacci(8) returns 13. fibonacci(1) returns 0;
*
* @param {number} n - which place in the sequence to return.
* starts at 1
*
* @return the nth number in the fibonnaci sequence.
*/
fibonacci(n) {
let fibs = [0,1];
while (fibs.length<n) {
fibs.push(fibs[fibs.length-2]+fibs[fibs.length-1]);
}
return fibs[n-1];
}
/**
* Make calls to the fibonacci(n) function to get each number
* in the fibonacci sequence from 1..${count} and add them to
* the return array
*
* @param {number} count - how many numbers in the fibonacci sequence
* should be returned in the array
*
* @return {number[]} the first ${count} numbers in the fibonacci sequence
*
*/
fibonacciArray(count) {
let fibs = [];
while (fibs.length<count) {
fibs.push(this.fibonacci(fibs.length+1));
}
console.log(fibs);
return fibs;
}
...