forked from r-argentina-programa/introduccion-a-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solutions up to r-argentina-programa#15 out of r-argentina-programa#53 …
…algorithms
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |