Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…algorithms
  • Loading branch information
iAlgebra committed Nov 20, 2019
1 parent 94803b0 commit fd513cd
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tareas/clase-4/algoritmo-11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Desafío de programación #11: Calcula el promedio de todos los números en un array de números. (en una función)
*/

let numbers = [5, 10, 70, 30];

function calculateAverage(arr) {
let result = 0;
for (let i = 0; i < arr.length; i++) {
result += arr[i];
}
return (result / arr.length).toFixed(2);
}

console.log(calculateAverage(numbers));
23 changes: 23 additions & 0 deletions tareas/clase-4/algoritmo-12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Desafío de programación #12: Crea una función que reciba un array de números y devuelva un array conteniendo solo los números positivos
*/

let numbers = [5, -8, 47, 36, 90, -5, -18, 0, 1, -4];

function returnsOnlyPositiveNumbers(arr) {
let positiveArray = [];
let text = '';
for (let i = 0; i < arr.length; i++) {
if (Math.sign(arr[i]) === 1) {
positiveArray.push(arr[i]);
} else if (Math.sign(arr[i]) === 0) {
text = 'zero is a neutral number';
}
}
return {
result: positiveArray,
additionalInfo: text,
};
}

console.log(returnsOnlyPositiveNumbers(numbers));
17 changes: 17 additions & 0 deletions tareas/clase-4/algoritmo-13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Desafío de programación #13: Find the maximum number in an array of numbers
*/

let listOfNumbers = [100, 3000, -852, 69, 5000, -5900];

function findTheMaxNumber(arr) {
let maxNum = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > maxNum) {
maxNum = arr[i];
}
}
return maxNum;
}

console.log(findTheMaxNumber(listOfNumbers));
21 changes: 21 additions & 0 deletions tareas/clase-4/algoritmo-14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Desafío de programación #14: Imprimir los primeros 10 dígitos de Fibonacci sin recursión
Fibonacci (lo buscan en google si no lo saben o no se acuerdan)
El resultado debería ser: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*/

function calculatesFibonacciSequence() {
let fibonacciSequence = [];
for (let i = 0; i <= 10; i++) {
if (i === 0) {
fibonacciSequence.push(i);
} else if (i === 1) {
fibonacciSequence.push(i);
} else {
fibonacciSequence.push(fibonacciSequence[i - 1] + fibonacciSequence[i - 2]);
}
}
return fibonacciSequence;
}

console.log(calculatesFibonacciSequence());
23 changes: 23 additions & 0 deletions tareas/clase-4/algoritmo-15.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Desafío de programación #15: Crear una función que encuentre el número n de Fibonacci usando recursión
*/

let fibonacciSequence = [];

function calculatesFibonacciSequence(i) {
if (i <= 10) {
if (i === 0) {
fibonacciSequence.push(i);
} else if (i === 1) {
fibonacciSequence.push(i);
} else {
fibonacciSequence.push(
fibonacciSequence[i - 1] + fibonacciSequence[i - 2]);
}
i++;
calculatesFibonacciSequence(i);
}
}

calculatesFibonacciSequence(0);
console.log(fibonacciSequence);

0 comments on commit fd513cd

Please sign in to comment.