8000 Add logic that shifts the current value and added value to the added … · rhawkenson/javascript-exercises@1124662 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1124662

Browse files
committed
Add logic that shifts the current value and added value to the added variables, and pushes the sum to an arry. Call the array value at the index of the one less than the num given to account for 0 index in arrays.
1 parent 9e3160f commit 1124662

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

fibonacci/fibonacci.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
const fibonacci = function() {
2-
1+
const fibonacci = function(num) {
2+
let current = 0;
3+
let newNum = 1;
4+
let resultsArr = [1];
5+
6+
if (!Number(num) || num < 0){
7+
return 'OOPS';
8+
9+
} else {
10+
for (let i=0; i<=num; i++){
11+
let value = current + newNum;
12+
resultsArr.push(value);
13+
current = newNum;
14+
newNum = value;
15+
16+
}
17+
}return resultsArr[num-1];
318
}
419

520
module.exports = fibonacci
21+
22+
// jasmine fibonacci.spec.js

fibonacci/fibonacci.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ describe('fibonacci', function() {
44
it('works', function() {
55
expect(fibonacci(4)).toEqual(3);
66
});
7-
xit('works', function() {
7+
it('works', function() {
88
expect(fibonacci(6)).toEqual(8);
99
});
10-
xit('works', function() {
10+
it('works', function() {
1111
expect(fibonacci(10)).toEqual(55);
1212
});
13-
xit('works', function() {
13+
it('works', function() {
1414
expect(fibonacci(15)).toEqual(610);
1515
});
16-
xit('works', function() {
16+
it('works', function() {
1717
expect(fibonacci(25)).toEqual(75025);
1818
});
19-
xit('doesn\'t accept negatives', function() {
19+
it('doesn\'t accept negatives', function() {
2020
expect(fibonacci(-25)).toEqual("OOPS");
2121
});
22-
xit('DOES accept strings', function() {
22+
it('DOES accept strings', function() {
2323
expect(fibonacci("1")).toEqual(1);
2424
});
25-
xit('DOES accept strings', function() {
25+
it('DOES accept strings', function() {
2626
expect(fibonacci("2")).toEqual(1);
2727
});
28-
xit('DOES accept strings', function() {
28+
it('DOES accept strings', function() {
2929
expect(fibonacci("8")).toEqual(21);
3030
});
3131
});

0 commit comments

Comments
 (0)
0