├── fibonacci-recursive.js ├── fibonacci.js ├── largest-element.js ├── math.js ├── max.js ├── sum.js ├── summary.js └── swap.js /fibonacci-recursive.js: -------------------------------------------------------------------------------- 1 | // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ] 2 | /* 3 | 3rd = 2nd + 1st 4 | 4th = 3rd + 2nd 5 | 5th = 4th + 3rd 6 | 6th = 5th + 4th 7 | 16th = 15th + 14th 8 | 119th = 118th + 117th 9 | nth = (n-1)th + (n-2)th 10 | ith = (i-1)th + (i-2)th 11 | */ 12 | /* 13 | function fibonacci(n) { 14 | if (n == 0) { 15 | return 0; 16 | } 17 | if (n == 1) { 18 | return 1; 19 | } 20 | return fibonacci(n - 1) + fibonacci(n - 2); 21 | } 22 | const fiboElement = fibonacci(6); 23 | console.log(fiboElement); 24 | 25 | */ 26 | 27 | 28 | function fibonacciSeries(n) { 29 | if (n == 0) { 30 | return [0]; 31 | } 32 | if (n == 1) { 33 | return [0, 1]; 34 | } 35 | const fibo = fibonacciSeries(n - 1); 36 | fibo[n] = fibo[n - 1] + fibo[n - 2]; 37 | return fibo; 38 | } 39 | const fiboSeries = fibonacciSeries(13); 40 | console.log(fiboSeries); -------------------------------------------------------------------------------- /fibonacci.js: -------------------------------------------------------------------------------- 1 | // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ] 2 | /* 3 | 3rd = 2nd + 1st 4 | 4th = 3rd + 2nd 5 | 5th = 4th + 3rd 6 | 6th = 5th + 4th 7 | 16th = 15th + 14th 8 | 119th = 118th + 117th 9 | nth = (n-1)th + (n-2)th 10 | ith = (i-1)th + (i-2)th 11 | */ 12 | /* 13 | const fibo = [0, 1] 14 | for (let i = 2; i <= 10; i++) { 15 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 16 | } 17 | console.log(fibo); 18 | */ 19 | /* 20 | function fibonacciSeries(num) { 21 | const fibo = [0, 1]; 22 | for (let i = 2; i <= num; i++) { 23 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 24 | } 25 | return fibo; 26 | } 27 | 28 | const fiboSeries = fibonacciSeries([234]); 29 | console.log(fiboSeries); 30 | */ 31 | 32 | function fibonacciSeries(num) { 33 | if (typeof num != 'number') { 34 | return 'Please give a number'; 35 | } 36 | if (num < 2) { 37 | return 'Please enter a positive number greater than 1' 38 | } 39 | const fibo = [0, 1]; 40 | for (let i = 2; i < num; i++) { 41 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 42 | } 43 | return fibo; 44 | } 45 | 46 | const fiboSeries = fibonacciSeries(13); 47 | console.log(fiboSeries); 48 | -------------------------------------------------------------------------------- /largest-element.js: -------------------------------------------------------------------------------- 1 | function largestElement(numbers) { 2 | let largest = numbers[0]; 3 | for (let i = 0; i < numbers.length; i++) { 4 | const element = numbers[i]; 5 | if (element > largest) { 6 | largest = element; 7 | } 8 | } 9 | return largest; 10 | } 11 | 12 | const ages = [12, 54, 2, 34, 75, 32, 12]; 13 | const oldest = largestElement(ages); 14 | const oldest2 = largestElement([-12, -6, -17]); 15 | console.log('oldest2', oldest2); 16 | 17 | // Task: find the lowest or smallest element of an array -------------------------------------------------------------------------------- /math.js: -------------------------------------------------------------------------------- 1 | // const myNumber = -5; 2 | // const output = Math.abs(myNumber); 3 | 4 | const myNumber = 21.95; 5 | // const output = Math.ceil(myNumber); 6 | // const output = Math.floor(myNumber); 7 | // const output = Math.round(myNumber); 8 | 9 | // const output = Math.random() * 6; 10 | // const rounded = Math.round(output); 11 | // console.log(rounded); 12 | 13 | for (let i = 0; i <= 30; i++) { 14 | const output = Math.random() * 6; 15 | const rounded = Math.round(output); 16 | console.log(rounded); 17 | } -------------------------------------------------------------------------------- /max.js: -------------------------------------------------------------------------------- 1 | const business = 1650; 2 | const minister = 850; 3 | const army = 900; 4 | // if (business > minister) { 5 | // console.log('Business person er pola is bigger'); 6 | // } 7 | // else { 8 | // console.log('minister er pola is bigger'); 9 | // } 10 | 11 | function findLargest(first, second) { 12 | if (first > second) { 13 | return first; 14 | } 15 | else { 16 | return second; 17 | } 18 | } 19 | 20 | const largest = findLargest(154, 241); 21 | console.log('largest is', largest) 22 | 23 | // compare 3 24 | /* if (business > minister && business > army) { 25 | console.log('Business is bigger'); 26 | } 27 | else if (minister > business && minister > army) { 28 | console.log('minister is bigger'); 29 | } 30 | else { 31 | console.log('army is bigger'); 32 | } */ 33 | 34 | // Task 1: create a function that takes three numbers as input parameter and returns you the largest number of the three 35 | 36 | var max = Math.max(business, minister, army); 37 | // console.log('largest is', max); 38 | 39 | // Task 2: Write a function to find the smallest of three numbers. -------------------------------------------------------------------------------- /sum.js: -------------------------------------------------------------------------------- 1 | const numbers = [44, 23, 34, 32, 54, 5, 78]; 2 | let sum = 0; 3 | for (let i = 0; i < numbers.length; i++) { 4 | const element = numbers[i]; 5 | sum = sum + element; 6 | } 7 | // console.log(sum); 8 | 9 | function arrayTotal(numbers) { 10 | let sum = 0; 11 | for (let i = 0; i < numbers.length; i++) { 12 | const element = numbers[i]; 13 | sum = sum + element; 14 | } 15 | return sum; 16 | } 17 | 18 | const total = arrayTotal([32, 45, 67]); 19 | console.log('array total', total) -------------------------------------------------------------------------------- /summary.js: -------------------------------------------------------------------------------- 1 | const number = 589.2356; 2 | // Math.floor 3 | // Math.ceil 4 | // Math.round 5 | /* 6 | const random = Math.random(); 7 | const between10 = random * 100; 8 | const rounded = Math.round(between10); 9 | console.log(rounded); 10 | */ 11 | const selected = [] 12 | for (let i = 0; i < 10; i++) { 13 | const random = Math.random() * 100; 14 | const picked = Math.round(random); 15 | if (selected.indexOf(picked) == -1) { 16 | selected.push(picked); 17 | } 18 | else { 19 | console.log('found duplicate', selected, picked) 20 | } 21 | } 22 | console.log(selected); -------------------------------------------------------------------------------- /swap.js: -------------------------------------------------------------------------------- 1 | var first = 5; 2 | var second = 7; 3 | console.log(first, second); 4 | // first approach 5 | // var temp = first; 6 | // first = second; 7 | // second = temp; 8 | // console.log(first, second); 9 | 10 | // destructuring 11 | [first, second] = [second, first]; 12 | console.log(first, second); --------------------------------------------------------------------------------