"Fibonacci Sequence" redirects here. For the chamber ensemble, see Fibonacci Sequence (ensemble) A tiling with squares whose side lengths are successive Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, and 21.
In mathematics, the Fibonacci numbers commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,[1]{\displaystyle F_{0}=0,\quad F_{1}=1,}
and{\displaystyle F_{n}=F_{n-1}+F_{n-2}}
for n > 1.
The beginning of the sequence is thus
functionfibonacci(maxNumber) {constfib= [];let fibRes = []; fib[0] =0; fib[1] =1; for (let i =2; i <= maxNumber; i++) { fib[i] = fib[i -2] + fib[i -1];fibRes.push(fib[i]); }console.log(fibRes);}
Practice 2
Create a function that will receive two arrays and will return an array with elements that are in the first array but not in the second.
functionelementSelector(arr1, arr2) {let myArray = [];for (let index =0; index <arr2.length; index++) {if (!arr1.includes(arr2[index])) {myArray.push(arr1[index]); } }console.log(myArray);}
Practice 3
Create a function that will receive two arrays of numbers as arguments and return an array composed of all the numbers that are either in the first array or second array but not in both.
functionmergeArrays(arr1, arr2) {let myArray = [];for (let index =0; index <arr2.length; index++) {if (!arr1.includes(arr2[index])) {myArray.push(arr2[index]); } }for (let index =0; index <arr1.length; index++) {if (!arr2.includes(arr1[index])) {myArray.push(arr1[index]); } }console.log(myArray);}
Calculate the average of the numbers in an array of numbers
functioncalculateAverage(arr) {let total =0;let n =arr.length;for (let i =0; i < n; i++) { total += arr[i]; } average = (total / n);return (Math.round(average));}
Practice 6
Create a function that will receive n as argument and return an array of n random numbers from 1 to n.
// Create a function that will receive n as argument and return an array of n// random numbers from 1 to n. The numbers should be unique inside the array.functionrandomize(num) {let myArray = [];for (let i =1; i <= num; i++) {if (!myArray.includes(i)) {myArray.push(i); } }return myArray;}
Practice 7
Reverse a string
// Reverse a stringfunctionreverseString(word) {let myWord ="";for(let i =word.length-1; i >=0; i--) { myWord += word[i]; }return myWord;}
Practice 8
Reverse an array using build-in method and non-build in.
// Reverse an arrayfunctionreverseArray(arr) {// return arr.reverse(); // Build-in methodlet myArray = []; for (let i =arr.length-1; i >=0; i--) {myArray.push(arr[i]); }return myArray;}
Practice 9
Create a function that receives an array of numbers as argument and returns an array containing only the positive numbers
// Create a function that receives an array of numbers as argument and returns an// array containing only the positive numbersfunctiongetPositiveNumbers(numArr) {let myArray = [];// First way// for (let i = 0; i < numArr.length; i++) {// if (numArr[i] > 0) {// myArray.push(numArr[i]);// }// }// Second way myArray =numArr.filter(x => x >0);return myArray;}