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#29 out of r-argentina-programa#53 …
…algorithms
- Loading branch information
Showing
4 changed files
with
131 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,27 @@ | ||
/* | ||
Desafío de programación #26: Crear una función que reciba dos arrays de números | ||
y retorne un nuevo array con los elementos que se encuentren en el primer array, | ||
pero no en el segundo | ||
Nota; Esto se llama "resta" entre conjuntos | ||
Ejemplo: | ||
[5,1,2,3,4] y [3,4] debería devolver [5,1,2] | ||
*/ | ||
|
||
let arr1 = [1, 2, 3, 10, 5, 3, 14]; | ||
let arr2 = [-1, 4, 5, 6, 14]; | ||
|
||
// let arr1 = [5, 1, 2, 3, 4]; | ||
// let arr2 = [3, 4]; | ||
|
||
function subtraction(arr1, arr2) { | ||
for (let i = 0; i < arr1.length; i++) { | ||
for (let j = 0; j < arr2.length; j++) { | ||
if (arr2[j] === arr1[i]) { | ||
arr1.splice(i, 1); | ||
} | ||
} | ||
} | ||
return arr1; | ||
} | ||
|
||
console.log(subtraction(arr1, arr2)); |
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,26 @@ | ||
/* | ||
Desafío de programación #27: Crear una función que reciba un array de números | ||
como argumento y retorne un array con los elementos distintos | ||
Ejemplo: | ||
[1,2,3,4,5,4,3,2,1,0] debería retornar [1,2,3,4,5,0] | ||
*/ | ||
|
||
const list = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; | ||
const list2 = [1, 2, 3, 6, -1, 2, 9, 7, 10, -1, 100]; | ||
|
||
function removeRepeated(arr) { | ||
for (let i = 0; i < arr.length; i++) { | ||
for (let j = 0; j < arr.length; j++) { | ||
if (i === j) { | ||
continue; | ||
} | ||
|
||
if (arr[i] === arr[j]) { | ||
arr.splice(j, 1); | ||
} | ||
} | ||
} | ||
return arr; | ||
} | ||
|
||
console.log(removeRepeated(list2)); |
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,34 @@ | ||
/* | ||
Desafío de programación #28: Calculate the sum of first 100 prime numbers | ||
*/ | ||
|
||
function isPrimeNumber(n) { | ||
if (n < 2) { | ||
return false; | ||
} else if (n === 2) { | ||
return true; | ||
} else { | ||
for (let i = 2; i < n; i++) { | ||
if (n % i === 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
function sumPrimeNumbers(numbersToSum) { | ||
let primeCounter = 0; | ||
let n = 0; | ||
let sumResult = 0; | ||
while (primeCounter < numbersToSum) { | ||
if (isPrimeNumber(n)) { | ||
primeCounter++; | ||
sumResult += n; | ||
} | ||
n++; | ||
} | ||
return sumResult; | ||
} | ||
|
||
console.log(sumPrimeNumbers(100)); |
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,44 @@ | ||
/* | ||
Desafío de programación #29: Print the distance between the first 100 prime numbers | ||
*/ | ||
|
||
function isPrimeNumber(n) { | ||
if (n < 2) { | ||
return false; | ||
} else if (n === 2) { | ||
return true; | ||
} else { | ||
for (let i = 2; i < n; i++) { | ||
if (n % i === 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
function printPrimeNumbers(numbersToPrint) { | ||
let primeCounter = 0; | ||
let n = 0; | ||
let primeNumbers = []; | ||
while (primeCounter < numbersToPrint) { | ||
if (isPrimeNumber(n)) { | ||
primeCounter++; | ||
primeNumbers.push(n); | ||
} | ||
n++; | ||
} | ||
return primeNumbers; | ||
} | ||
|
||
console.log(printPrimeNumbers(100)); | ||
|
||
function distances() { | ||
const primeNumbers = printPrimeNumbers(100); | ||
for (let i = 0; i < primeNumbers.length - 1; i++) { | ||
console.log(primeNumbers[i + 1] + ' - ' + primeNumbers[i] + ' = ' + | ||
(primeNumbers[i + 1] - primeNumbers[i])); | ||
} | ||
} | ||
|
||
distances(); |